Why can I change the value of a const char* variable?

You are changing the pointer, which is not const (the thing it’s pointing to is const).

If you want the pointer itself to be const, the declaration would look like:

char * const str = "something";

or

char const * const str = "something";  // a const pointer to const char
const char * const str = "something";  //    same thing

Const pointers to non-const data are usually a less useful construct than pointer-to-const.

Leave a Comment