Character array literals
The place where strict constness
is not enforced is with character array
literals. You can
say
char* cp = "howdy";
and the compiler will accept it without
complaint. This is technically an error because a character array literal
(“howdy” in this case) is created by the compiler as a
constant character array, and the result of the quoted character array is its
starting address in memory. Modifying any of the characters in the array is a
runtime error, although not all compilers enforce this
correctly.
So character array literals are actually
constant character arrays. Of course, the compiler lets you get away with
treating them as non-const because there’s so much existing C code
that relies on this. However, if you try to change the values in a character
array literal, the behavior is undefined, although it will probably work on many
machines.
If you want to be able to modify the
string, put it in an array:
char cp[] = "howdy";
Since compilers often don’t enforce
the difference you won’t be reminded to use this latter form and so the
point becomes rather
subtle.