Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed *x = 3 in compliant example

...

Code Block
bgColor#FFCCCC
void foo(int* x) {
  if (x != NULL) {
    *x = 3; /* visible outside function */
  }
  /* ... */
}

Non-Compliant Code Example

If a function modifies a pointed-to value, declaring this value as const will be caught by the compiler.

Code Block
bgColor#ffffcc

void foo(const int * x) {
  if (x != NULL) {
    *x = 3; /* generates compiler warning */
  }
  /* ... */
}

Compliant Solution

If a function does not modify the pointed-to value, it should declare this value as const. This improves code readability and consistency.

Code Block
bgColor#ccccff
void foo(const int * x) {
  if (x != NULL) {
    *x = 3; /* visible outside function */printf("Value is %d\n", *x);
  }
  /* ... */
}

Non-Compliant Code Example

This non-compliant code example, defines a fictional version of the standard strcat() function called strcat_nc(). This function differs from strcat() in that the second argument is not const-qualified.

...