const with pointers has some subtle aspects (pointers always seem to have subtle aspects), so let’s take a closer look. You can use the
const keyword two different ways with pointers. The first way is to make a pointer point to a constant object, and that prevents you from using the pointer to change the pointed-to value. The second way is to make the pointer itself constant, and that prevents you from changing where the pointer points. Now for the details.
First, let’s declare a pointer
pt that points to a constant:
int age = 39;const int * pt = &age;
This declaration states that
pt points to a
const int (
39, in this case). Therefore, you can’t use
pt to change that value. In other words, the value
const and cannot ...