Do not cast away a const qualification on a variable type. Casting away the const qualification allows violation of rule VOID EXPxx-C. Do not modify constant values, which prohibits the modification of constant valuesa program to modify a constant value. The C99 standard states:
If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.
As an illustration, C99 provides a footnote:
The implementation may place a const object that is not volatile in a read-only region of storage. Moreover, the implementation need not allocate storage for such an object if its address is never used.
Non-Compliant Code Example
...
| Wiki Markup |
|---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.3, "Type qualifiers"
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "HFC Pointer casting and pointer type changes"
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 11.5
, in section 6.7.3, item 5: |
...
EXP04-A. Do not perform byte-by-byte comparisons between structures 03. Expressions (EXP) EXP06-A. Operands to the sizeof operator should not contain side effects