Thursday, 20 March 2014

HOW TO USE CONST QUALIFIER WITH POINTERS

  • const qualifier – variable cannot be changed
–Good idea to have const if function does not need to change a variable.
–Attempting to change a const is a compiler error.
  • const pointers – point to same memory location
-Must be initialized when declared.
int *const myPtr = &x;
  • Type int *const – constant pointer to an int
const int *myPtr = &x;
  • Regular pointer to a const int
const int *const Ptr = &x;
  • const pointer to a const int
  • x can be changed, but not *Ptr

No comments:

Post a Comment