Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
bgColor#FFcccc
langc
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
bgColor#FFcccc
langc
const char s[] = "foo";
int main() {
  *(char*)s = '\0';
}

...