Cpp - Pointer const Pointers

Introduction

You can use the keyword const for pointers before the type, after the type, or in both places.

For example, all the following are legal declarations:

const int *pOne; 
int * const pTwo; 
const int * const pThree; 

These three statements mean the different things.

pOne is a pointer to a constant integer. The value that is pointed to can't be changed using this pointer. That means you can't write the following:

*pOne = 5; 

pTwo is a constant pointer to an integer. The integer can be changed, but pTwo can't point to anything else. A constant pointer can't be reassigned. That means you can't write this:

pTwo = &x; 

pThree is a constant pointer to a constant integer. The value that is pointed to can't be changed and pThree can't be changed to point to anything else.

If the word const is to the left of the *, that means the object is constant.

If the word const is to the right of the *, the pointer itself is constant:

const int *p1;   // the int pointed to is constant 
int * const p2;  // p2 is constant, it can't point to anything else