line 22: >> scanf("Selection: %i\n", o);
You can't combine scanf() and printf() like that, and the parameter must be a pointer to an integer
printf("Selction: ");
scanf("%i", &o);
Ancient Dragon
Retired & Loving It
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
That dismissed one of the warnings, but I still get a segmentation fault at
reply->option=o;
Why is that even a problem?
struct REPLY *reply; is a pointer, anything you try to write to it it will produce a segmentation fault, since there's no memory allocated for it.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
I'm never sure when to use malloc and when C allocates the memory for me.
For some, memory management in C, is a curse, nevertheless it is one of the strength of the language.All of the other warnings look like this:
menu.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[256]’
After allocating memory for it, remove any & in front of those pointers to string like
scanf("%s\n", &reply->pass);
and the '\n' is not going to give you the intended result, neither.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
char *c; is a pointer, again, it doesn't point to any proper memory allocated to it. while(fgets(c, 600, fin)!=NULL) Guess what you are trying to do there? Yeap! Trying to write to poor char *c that it doesn't have any proper memory available for it.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
Aw man, I thought C handled chars! It's just one byte! Okay, thanks again for your help.
As a general rule, do segmentation faults happen because memory isn't allocated? I'd guess with beginner C it wouldn't be anything fancier.
:) segmentation faults occur when you are trying to access memory that you are not suppose to.e.g.
int *x; /* declares a pointer to int, the variable x is holding a random value which will be used to represent an address */
If you don't make that x to point to something meaningful, who knows what that random value is going to try to represent, and access as an address. Accessing that random address to write into, can create a segmentation fault.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218