char* name;
That declares name
to be a pointer. But it is uninitialized.
gets (name);
Tries to read input into whatever name
points to. Since name
has not been initialized to point to valid memory, trying to read data into what name
points to is undefined behavior. Your program can do anything.
When you use:
char* name = (char *) calloc(100, sizeof(char));
name
points to a location that can hold 100 characters. If your input is less than 100 characters (leave one for the terminating null characters), then
gets(name);
will be fine. If the input is 100 or more characters, your program will be subject to undefined behavior again. That's why use of gets
is considered a security risk. DON'T USE IT. For further information see Why is the gets function so dangerous that it should not be used?.
Instead, use
fgets(name, 100, stdin);