There is no "hidden storage". These lines
const char c = 'C'; char * p = const_cast<char *>(&c);
seriously violate const correctness . You are creating a non const pointer to what const was originally. Never do that. . Although the translation itself is fine, if you try to dereference p , it will cause undefined behavior, which means that something can happen, including the behavior that you just described.
Speaking of what happens, the compiler adds the constant c to print the first cout c statement. Therefore, the compiler probably included the following cout expressions in it:
cout << " c = " << 'C' << endl; // Note literal 'C' instead of variable c cout << " *p = " << *p << endl;
So, while the second cout operator reflects the new value of c by dereferencing p , the first cout operator has no effect.
The first cout not affected, because the compiler assumed that the value of c would never change (this is const anyway). Based on this assumption, the compiler replaced access to the variable with the constant value itself.
You violated the compiler assumption when you did this:
*p = 'K';
since p points to the constant c , and you just changed it to K , giving you the behavior you see.
source share