| Wiki Markup |
|---|
According to C99 \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.3, "Type qualifiers," Paragraph 5 (see also [bulletundefined behavior 61 | CC. Undefined Behavior#unb_61] of Appendix J): |
...
It is also a recommended practice not to cast away a const qualification (see EXP05-C. Do not cast away a const qualification) because doing so makes it easier to modify a const-qualified value without warning.
...
Noncompliant Code Example
The following well-formed but non-compliant noncompliant code example borrowed from section 6.5.16.1 of C99 allows a constant value to be modified.
...
The first assignment is unsafe because it would allow the valid code that follows to attempt to change the value of the const object c.
...
Noncompliant Code Example (modifying a string literal)
Similarly to the previous example, the well-formed but non-compliant 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.
...
The compliant solution depends on the intention of the programmer. If the intention is that the value of c is modifiable, then it should not be declared as a constant. If the intention is that the value of c is not meant to change, then do not write non-compliant noncompliant code that attempts to modify it.
...