1

I hope to get some advice. I thank you so much.

char *p = "abc";
printf("Value at the location the pointer points to = %s\n", p);
printf("The location where the pointer points to = %d\n", p);
printf("The location of the pointer = %d\n", &p);
char s[] = "Hello";
printf("Vi tri cua s = %d\n", &s);
gets(s);  //we must enter less than 5 characters, because we have s[5]
puts(s);
printf("The new value at the location the pointer points to:\n");
gets(p);  //This function cause problem, while the function gets(s) does not
puts(p);

1 Answers1

0

"abc" is a literal and is write-protected (read-only). Hence you can't read into it.

char s[] = "Hello"; creates an array and copies the literal string into it. The resulting array is not a literal, hence can be modified.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    "is write-protected (read-only)" + "with the vast majority of compilers". (I have not run into any where it isn't, but supposedly there is such an animal out in the wild) – David C. Rankin Nov 16 '18 at 12:30