Am I using scanf() in some wrong way?
char *input;
scanf("%s", input);
printf("%s\n", input);
This fails at the run-time.
Declaring a char *
only creates a pointer, it does not allocate any memory for the string. You need to allocate memory for input. You can do that dynamically via malloc
(and free
when done) or you can declare an array of static size like char input[100]
.
char *input;
This is a pointer. It doesn't point to any memory.
#include <stdlib.h>
#include <stdio.h>
int main()
{
//char *input;
char input[128];
memset(input, 0 ,sizeof(input));
scanf("%s", input);
printf("%s\n", input);
return 0;
}
replace char *input;
with char input[1024] = {0};
you should ensure the parameter you pass to scanf points to a buffer which could hold your input