Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 3. Declarations and Initializati... > DCL13-C. Declare function parameters...

DCL13-C. Declare function parameters that are pointers to values not changed by the function as const

Declaring function parameters const indicates that the function promises not to change these values.

In C, function arguments are passed by value rather than by reference. While a function may change the values passed in, these changed values are discarded once the function returns. For this reason, many programmers assume a function will not change its arguments and that declaring the function’s parameters as const is unnecessary.

void foo(int x) {
  x = 3; /* persists only until the function exits */
  /* ... */
}

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.

void foo(int *x) {
  x = NULL; /* persists only until the function exits */
  /* ... */
}

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint