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)
?
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 the keyboard.
int main() {
char arrays[12];
char *pointers = arrays;
scanf("%s", pointers);
printf("%s", pointers);
return 0;
}
char *pointers;
creates a pointer variable.pointers
is the address
pointed to by pointers
, which is indeterminate by
default.*pointers
is the data in the address pointed to by pointers
, which you cannot do until address is assigned.Just do this.
char arrays[12];
char *pointers;
pointers = arrays;
scanf("%s",pointers);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With