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

Winapi c++: DialogBox hangs when breaking a loop -

vb.net - Font adding using PDFsharp -

javascript - jQuery iScroll clickable list elements while retaining scroll? -