C++ Programming (using keyword const)

The keyword const can be used in two different ways:

• Non-constant pointer to constant data:
Pointer can points to other variable, but data to which it points can’t be modified.

• Constant pointer to non-constant data:
Pointer always point to the same memory location, but data can be modified.


int x=10, y=20;
const int* pXPtr = &x; //Non-constant pointer, constant data
int* const pYPtr = &y; //Constant pointer, non-constant data
pXPtr = &y; //Legal
*pXPtr = 20; //Error
pYPtr = &x; //Error
*pYPtr = 10; //Legal

More examples of using keyword const: