MISRAC++2023-10.1.1
Synopsis
(Advisory) The target type of a pointer or lvalue reference parameter should be const-qualified appropriately
Enabled by default
No
Severity/Certainty
Low/Medium

Full description
A parameter in a function that is not modified by the function is not const qualified. This check is identical to MISRAC++2008-7-1-2, MISRAC2004-16.7.
Coding standards
- MISRA C:2004 16.7
(Required) A pointer parameter in a function prototype should be declared as pointer to const if the pointer is not used to modify the addressed object.
- MISRA C++ 2008 7-1-2
(Required) A pointer or reference parameter in a function shall be declared as pointer to const or reference to const if the corresponding object is not modified.
Code examples
The following code example fails the check and will give a warning:
int example(int* x) { //x should be const
if (*x > 5){
return *x;
} else {
return 5;
}
}
The following code example passes the check and will not give a warning about this issue:
int example(const int* x) { //OK
if (*x > 5){
return *x;
} else {
return 5;
}
}