Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG REPORT] Using "s" to read in a string doesn't generate the proper C code #183

Closed
Martianmellow12 opened this issue Oct 3, 2020 · 6 comments
Assignees
Labels
bug Something isn't working KWoC'20 These issues are being listed as part of KWoC'20 moderate Moderate problem

Comments

@Martianmellow12
Copy link
Contributor

Describe the bug
When input is called with "s" as the input type, the generated C code doesn't work properly.

To Reproduce
Below is a simc program accompanied by the generated C program:

simc

MAIN
  var i = input("Enter a string: ", "s")
  print("You entered ")
  print(i)
  print("\n")
END_MAIN

Generated C

#include <stdio.h>

int main() {
	char* i;
	printf("Enter a string: ");
	scanf("%s", i);
	printf("You entered ");
	printf("%s", i);
	printf("\n");

	return 0;
}

Expected behavior
It's expected that whatever string was entered would be printed out, however all that's printed is (null). Looking at the C code, it can be seen that the pointer we're trying to read a string into (char* i) is only able to hold a single character, which is likely causing the null issue. To properly read in a string, char* i should instead be something like char i[256], or something similar (the exact size would depend on the size of the string being read in, so this implementation would require a limit on the maximum number of characters that can be read into the string).

Desktop (please complete the following information):

  • OS: Linux (Ubuntu)
@Martianmellow12 Martianmellow12 added the bug Something isn't working label Oct 3, 2020
@frankhart2018
Copy link
Member

@Martianmellow12 it seems that the issue only appears when compiled in linux, in macOS and Windows it works fine. Still it needs to be changed. But the issue with a constant length of 256 is -

  1. It cannot hold a string longer than 256 bytes.
  2. If the string is small let's assume "abc" then also the string holding it will take up 256 bytes in memory.

I believe there should be some other option. If you get one do send it in this thread, if any of us get it we will send it here.

@frankhart2018 frankhart2018 added difficult Difficult problem hacktoberfest Part of hacktoberfest labels Oct 4, 2020
@frankhart2018 frankhart2018 removed the hacktoberfest Part of hacktoberfest label Nov 1, 2020
@dhairyaj dhairyaj added KWoC'20 These issues are being listed as part of KWoC'20 moderate Moderate problem and removed difficult Difficult problem labels Dec 4, 2020
@Chasmiccoder
Copy link
Contributor

I think this is because the C compiled code is using the scanf( ) statement incorrectly. Instead of
scanf( "%s", &i);
we are getting:
scanf("%s", i );

image

@frankhart2018
Copy link
Member

You can take up this issue @Chasmiccoder.

@Chasmiccoder
Copy link
Contributor

Okay. I will try. @frankhart2018

@frankhart2018
Copy link
Member

Assigning this to you @Chasmiccoder.

@frankhart2018
Copy link
Member

Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working KWoC'20 These issues are being listed as part of KWoC'20 moderate Moderate problem
Projects
None yet
Development

No branches or pull requests

5 participants