- In ansi c, the String constant created during pointer Initialization is defined as read-only. If you try to modify the value of this string through a pointer, the program will be undefined. In some compilers, A String constant is stored in a text segment that can only be read to prevent modification.
- Arrays can also be initialized using string constants:
Char A [] = "abcdefg ";
If the pointer is the opposite, the array initialized by the String constant can be modified. A single character can be changed later.
As follows:
(1) modify the data in the pointer string
# Include <string. h> # include <stdio. h> int main () {char * Buf = "abcdefg"; printf ("Buf = % C \ n", Buf [2]); buf [2] = 'M'; printf ("end! \ N ");}
The compilation is successful, but an error occurs during the program running:
(2) modify the data in the array string
# Include <string. h> # include <stdio. h> int main () {char Buf [] = "abcdefg"; printf ("Buf = % C \ n", Buf [2]); buf [2] = 'M'; printf ("Buf = % s \ n", Buf); printf ("end! \ N ");}
You can see that the program runs normally:
(-------- End --------)