BartC said:
OK, but I would have expected some warning at least to have been added over
the last few decades, considering the number of minor matters that compilers
do pounce on.
The standard does not specify warnings. It requires *diagnostics* in
many cases, but in all those cases the diagnostics are permitted to be
fatal error messages.
Individual compilers may issue whatever additional warnings they like.
The gcc documentation explains the rationale for not warning about
non-const pointers to string literals by default:
These warnings will help you find at compile time code that can try
to write into a string constant, but only if you have been very
careful about using `const' in declarations and prototypes.
Otherwise, it will just be a nuisance. This is why we did not make
`-Wall' request these warnings.
(Personally, I think programmers *should* be very careful about using
"const", but that doesn't mean a compiler should enforce it by default.)
I've used gcc -Wall -Wpedantic -Wextra, and not a peep out of it!
(Apparently, -Wwrite-strings is needed to enable the warning; I only
discovered that by running g++ which does warn.)
(I'm not that concerned, just wondering how seriously compilers take the
issue of const qualifiers. Because if I can write:
char* q;
q="ABC";
*q='X'; /* Crashes in Windows and Linux */
with nothing at all emitted by the compiler unless I go considerably out of
my way, then the answer seems to be not very. It just seems a gaping
loop-hole in the const-qualifier system.)
Yes, it's a gaping loophole in the const-qualifier system.
It's unfortunate that it wasn't practical to close it when "const"
was added to the language by the 1989 ANSI C standard, but we're
stuck with it.
The strchr() and memchr() functions can also be used to violate
const-correctness, since they can quietly return a non-const pointer
into a const array. That could have been fixed by splitting both
functions into a const version and a non-const version. (C++
uses overloading to do that.)
I'm not aware of any other such loopholes, but I could be missing
something.
There's not much point in arguing that this is a flaw in the
language; I think everyone here agrees with you. We're not defending
the rule, we're merely explaining why it (unfortunately) exists.