Const Pointers

We have seen the keyword const used in several contexts so far this semester -- const integers, const member functions, and reference to const objects as parameters. Now it's time to look at using const with pointers.

Note that although these example uses integers and pointers to integers, the same principles apply to objects and pointers to objects. For objects, "changing" the object usually means invoking a non-const member function.

Pointer to a Constant

We are familiar with the declaration of a const int such as const int age = 42; and we know that any attempt to modify age will result in a compiler error.

What if we define a pointer to age, in an attempt to circumvent this restriction? Should this be allowed?

const int age = 42; int *pAge = &age; *pAge = 57; It's not feasible for the compiler to keep track of what a pointer actually points to, so it's not feasible for the compiler to allow such a pointer to be defined and then issue an error when the pointer is used in an attempt to change a const object. Rather, a compiler error is generated when we attempt to make a pointer to a non-const object point to a const object. In this case, the statement int *pAge = & age; will cause a compiler error rather than the statement *pAge = 57;

To allow indirect access to const ints , but not allow the pointer to be used to change the const int to which it points, we must declare the pointer as "pointer to const int".

const int age = 42; const int *pcInt = & age; cout << *pcInt << endl; // ok - just dereferencing *pcInt = 57; // compiler error The declaration of pcInt can be read from right to left as "pcInt is a pointer to variable of type int defined as const".

Interestingly, pcInt can be changed to point to a non-const integer, but any attempt to change the integer using pcInt still results in a compiler error.

const int age = 42; const int *pcInt = & age; // ok *pcInt = 57; // compiler error int height = 59; // non-const int pcInt = & height; // ok *pcInt = 60; // still a compiler error In the real world, pointers to const objects are most often used as function parameters, guaranteeing that the function won't change the object to which the pointer points.

Const Pointers

Sometimes we want to define a pointer that cannot be changed. That is, we define our pointer to point to an object and never change where the pointer points. If the object pointed to is not const, the pointer can be used to change it. int width = 56; int length = 42; int *const pLength = & length; // initialized and unchangeable *pLength = 86; // ok - length is not const pLength = &height; // compiler error - pLength is const

Const Pointers to Const Objects

When we combine the two previous definitions, we can create const pointers that point to const objects. In this case, neither the pointer nor what the object to which it points may be changed. int size = 43; // non-const int const int weight = 89; // const int const int *const pWeight = &weight; // const pointer to a const int *pWeight = 88; // compiler error pWeight = &size; // compiler error cout << *pWeight << endl; // ok - just dereferencing

Reading Pointer Definitions

There are two approaches to understanding pointer defintions.

One way to avoid being confused about const and pointers is to separate the definition into two parts, divided by the "*". The definition to the left of the * tells you what the pointer points to. The definition to the right of the * tells you the pointer's name and whether or not its const.

The other way is to simply read the definitions from left to right.

Using const in as many as two places in the defintion of a pointer means there are four possible ways to define a pointer. Write an English description of these definitions and explain whether or not the pointer can be changed and whether or not you can use the pointer to change what what it points to.

  1. int *pInt;
  2. const int *pInt;
  3. int *const pInt;
  4. const int *const pInt;

Dennis Frey
Last modified: Mon Dec 6 12:47:04 EST 2004