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 ...
Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.