Character pointers in C confuse me.
Suppose we have a char pointer that points to the first character of a string constant .
char *a="ABCD";
Then we cannot change the value of this symbol using the a pointer, since the following statement leads to a segmentation error.
*a='X';
Now suppose we have a char pointer that points to a constant character.
const char B='X'; char *ptr=&B;
Then we are allowed to change the value of this symbol using the operator
*ptr='Z';
My question is, is this a case of undefined behavior proving that C is not reliable? Or is there some deeper logic?
source share