I'm a newbie to c programming. I'm trying to input two strings using scanf. My first try was as below
#include <stdio.h>
int main(void)
{
char *word1;
char *word2;
scanf("%s", word1);
scanf("%s", word2);
printf("%s\n", word1);
printf("%s\n", word2);
}
If I run this code, only the first input is correctly stored (word2 is null). But, if I run the code below, both inputs are correctly stored in word1 and word2.
#include <stdio.h>
int main(void)
{
char word1[10];
char word2[10];
scanf("%9s", word1);
scanf("%9s", word2);
printf("%s\n", word1);
printf("%s\n", word2);
}
What is the problem with using pointers with scanf?