...
The following well-formed but noncompliant code example borrowed from section 6.5.16.1 of C99 allows a constant value to be modified.
| Code Block | ||||
|---|---|---|---|---|
| ||||
char const **cpp; char *cp; char const c = 'A'; cpp = &cp; /* constraint violation */ *cpp = &c; /* valid */ *cp = 'B'; /* valid */ |
...
Similarly to the previous example, the well-formed but noncompliant code example below modifies a constant object after casting away its constness. Compiling the program on a Linux/x64 system doesn't produce any diagnostics even at high warning levels but the generated executable program fails at runtime with SIGESGV.
| Code Block | ||||
|---|---|---|---|---|
| ||||
const char s[] = "foo";
int main() {
*(char*)s = '\0';
}
|
...