here's the offending code using ubuntu
char *name;
int main(void)
{
fgets(name, sizeof(name), stdin);
}
void HUD()
{
printf("%s ", name);
}
Here's my problem. I started with scanf("%s", &name) and was getting junk at the end of the string. Through the last 2 hours have been reading docs on scanf, and fgets, because apparently scanf shouldn't be used when you don't know the size of the array you want, (and since user input can vary in size) I decided to try using fgets. I've also tried setting a fixed value both by char name[100]; and by fgets(name, 100, stdin)
Now I'm getting a segmentation fault, and through reading every result I found on the first 2 pages of google, my syntax appears correct, and I've found nothing on cboard or here to fix my problem.
Any ideas?
scanf()
just fine when you don't know the length of the input, as long as you specify a field width that prevents your array from being overrun.fgets()
has exactly the same limitation; it's just more explicit about making you tell it the limit on how many characters it should read. – John Bollingerscanf()
andfgets()
differ on how to determine when to stop reading. – John Bollinger