const pointer and address

A tutorial says this:

"A const pointer always points to the same address, and this address can not be changed. "

But then says that this is ok:

int nValue = 5;
int nValue2 = 6;

const int *pnPtr = &nValue;
pnPtr = &nValue2; // okay

It seems to contradict. The address of pnPtr changes from the address space of &nValue to the address space of &nValue2, yet pnPtr is a constant pointer.

What am I missing?
pnPtr is a pointer to a const int. The pointer itself is not const. So, you can make pnPtr point at something else as you've done above, but you can't use it to change what is being pointed to.

To illustrate:
1. Non-const pointer to non const (both address and value can change):
int *ptr;
2. Pointer to const (address can change, but not value, this is what you have above):
const int *ptr_to_cnst;
3. Const pointer to non-const (address cannot change, but value can change, this is what your tutorial meant):
int *const cnst_ptr;
4. Const pointer to const (neither address nor value can change):
const int *const cnst_ptr_to_cnst;

Note that the following is also not a const pointer but is a pointer to const:
int const *ptr_to_const;
Topic archived. No new replies allowed.