man/checkers/constParameterPointer.md
Message: Parameter 'x' can be declared as pointer to const
Category: Robustness
Severity: Style
Language: C/C++
This checker identifies function parameters that are pointers which are never used to modify the data they point to. Such parameters can be declared as "pointer to const" to improve code clarity and prevent accidental modifications.
The checker analyzes pointer parameters and detects when:
This warning helps improve code quality by:
This checker improves const-correctness. Const-correct code is more robust and easier to understand.
Add the const keyword to the parameter declaration to indicate that the pointed-to data will not be modified.
Before:
void printValue(int* p) {
printf("%d\n", *p); // Only reading the value
}
int findMax(int* arr, size_t size) {
int max = arr[0];
for (size_t i = 1; i < size; i++) {
if (arr[i] > max) { // Only reading array elements
max = arr[i];
}
}
return max;
}
bool isEqual(char* str1, char* str2) {
return strcmp(str1, str2) == 0; // Only reading strings
}
After:
void printValue(const int* p) {
printf("%d\n", *p); // Clearly indicates read-only access
}
int findMax(const int* arr, size_t size) {
int max = arr[0];
for (size_t i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
bool isEqual(const char* str1, const char* str2) {
return strcmp(str1, str2) == 0; // Standard library functions expect const char*
}
constParameter - for non-pointer parameters that can be constconstParameterReference - for reference parameters that can be constconstParameterCallback - for callback function parameters that can be constconstVariablePointer - for local pointer variables that can be const