char* pstr[] = { "Robert Redford", // Initializing a pointer array
"Hopalong Cassidy",
"Lassie",
"Slim Pickens",
"Boris Karloff",
"Oliver Hardy"
};
If I write like below:
*pstr[0] = 'X';
The program can compile but crashes when this statement is executed. Why? I thought *pstr[0] is ‘R’ so that I can change from ‘R’ to ‘X’.
Thanks!
The compiler should have warned you:
What you’re doing here is assigning some constant char arrays to a mutable char pointer, much like:
The result is, at run time, a pointer that points into your binary, and that you’re writing to. But that’s readonly memory, so.. crash.