...
In this noncompliant code example, the comparison of a to b has no effect.
| Code Block |
|---|
|
int a;
int b;
/* ... */
a == b;
|
...
The assignment of b to a is now properly performed.
| Code Block |
|---|
|
int a;
int b;
/* ... */
a = b;
|
...
In this example, a pointer increment and then a dereference occurs. However, the dereference has no effect.
| Code Block |
|---|
|
int *p;
/* ... */
*p++;
|
Compliant Solution (Dereference)
Correcting this example depends on the intent of the programmer. For instance, if dereferencing p was a mistake, then p should not be dereferenced.
| Code Block |
|---|
|
int *p;
/* ... */
p++;
|
If the intent was to increment the value referred to by p, then parentheses can be used to ensure p is dereferenced and then incremented. (See recommendation EXP00-C. Use parentheses for precedence of operation.)
| Code Block |
|---|
|
int *p;
/* ... */
(*p)++;
|
...
Another possibility is that p is being used to reference a memory-mapped device. In this case, the variable p should be declared as volatile.
| Code Block |
|---|
|
volatile int *p;
/* ... */
(void) *p++;
|
...