Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this noncompliant code example, the comparison of a to b has no effect.

Code Block
bgColor#FFCCCC
langc
int a;
int b;
/* ... */
a == b;

...

The assignment of b to a is now properly performed.

Code Block
bgColor#ccccff
langc
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
bgColor#FFCCCC
langc
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
bgColor#ccccff
langc
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
bgColor#ccccff
langc
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
bgColor#ccccff
langc
volatile int *p;
/* ... */
(void) *p++;

...