Since scanf does not know how long the array takes. The variable "name" does not refer to the type of "array", but refers to the type of "pointer" (or "address"). He says, start writing here and keep writing until you are done. You might be lucky and you have something else non-critical in your stack that will be overwritten, but ultimately scanf will write and write and overwrite something fatal and you will get a segmentation error. That is why you should always pass the size of arrays.
It is like giving a blind person a pencil and saying, βStart writing here,β without being able to see where the paper ends. In the end they will write on the table and damage something. (Note: this is not a blind knock, it is just a metaphor.)
In the above case, I highly recommend using fgets () to grab a certain amount from stdin and then sscanf () to pull any information from this line and put it in separate variables as needed. Scanf () and fscanf () are evil, I have never found for them that fgets () + sscanf () cannot solve it more safely.
char line[1024]; if( fgets( line, 1024, stdin ) != NULL ) { fprintf( stdout, "Got line: %s", line ); }
Or for things behind the lines:
# cat foo.c
source share