using scanf function with pointers to character

Saurabh.V picture Saurabh.V · Jan 27, 2013 · Viewed 62.7k times · Source

I have written the following piece of code.

int main(){
   char arrays[12];
   char *pointers;
   scanf("%s",arrays);
   scanf("%s",pointers);
   printf("%s",arrays);
   printf("%s",pointers);
   return 0;
}

Why does it give an error when I write `scanf("%s",pointers)?

Answer

Junior Fasco picture Junior Fasco · Jan 27, 2013
char *pointers; 

must be initialized.You can not scan string into pointers until you point it to some address. The computer needs to know where to store the value it reads from key board.

int main(){
   char arrays[12];
   char *pointers= arrays;
   scanf("%s",pointers);
   printf("%s",pointers);
   return 0;
}