...
Code Block |
---|
void foo(int x) { x = 3; /* visibleVisible only in the function. */ /* ... */ } |
Pointers behave in a similar fashion. A function may change a pointer to reference a different object, or NULL
, yet that change is discarded once the function exits. Consequently, declaring a pointer as const
is unnecessary.
Code Block |
---|
void foo(int *x) {
x = NULL; /* Visible only in the function. */
/* ... */
}
|
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
void foo(int *x) { if (x != NULL) { *x = 3; /* visibleVisible outside function. */ } /* ... */ } |
If the function parameter is const
-qualified, any attempt to modify the pointed-to value should cause the compiler to issue a diagnostic message.
Code Block | ||||
---|---|---|---|---|
| ||||
void foo(const int *x) { if (x != NULL) { *x = 3; /* compilerCompiler should generate diagnostic message. */ } /* ... */ } |
As a result, the const
violation must be resolved before the code can be compiled without a diagnostic message being issued.
...