Alex Rivera | Logout

using scanf function with pointers to character

Asked 2013-01-27T09:17:26.770
17

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)?

Edit
Report

1 Answer

5
  • 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);
answered 2013-01-27T09:23:42.350

Your Answer