O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

const Pointers

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;

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

*pOne = 5

If you try to do so, the compiler will object with an error.

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

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required