scanf() expects a pointer to a buffer where the string read from stdin will be copied to. If you do this:
Code:
char *name;
scanf("%s", name);
scanf will do its work, but you'll get, probably, a "buffer overrun" (no buffer defined by the pointer may poison the stack) or an "segmentation fault".
Since "name" can have any number of chars, I recommend:
Code:
// This will read from stdin allocating the buffer automatically...
char *name = NULL;
size_t size = 0;
if ( getline( &name, &size, stdin ) == -1 )
{
perror("getline");
exit(1);
}
// get rid of the final '\n', if there is one.
if (size && name[size-1] == '\n') name[size-1]='\0';
// Here name points to a buffer with your string.
// Need to be free() later.
getline() is POSIX.1-2008 standard.