c - input from fgets not stored using sscanf properly -


so have small part of code (basically input whole program).

it has take form of string @ 20 characters, , 2 integers separated space.

eg master 1 1

int main(void) {    int i, x, y;   char input[28];   char* name = null;   fgets(input, 28, stdin);   sscanf(input, "%s %i %i", name, &x, &y);   printf("%s %i %i\n", name, x, y);   return 0; } 

basically i'm trying print out see if program storing input correctly, has stored in linked list later on.

everything worked fine, no matter type, prints out (null) 0 4196064. seg faults if put , ampersand infront of name in sscanf() function well.

any ideas on how fix appreciated!

sscanf not allocate storage string.

char name[28]; 

will work. &name causes seg.fault, because differs in levels of indirection. pass char * * , interpreted char * inside sscanf. name variable on stack, &name points stack, sscanf overwrite stack content. can result in seg.fault.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -