Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
A constant pointer is, like any other constant variable, is used to store constants. But since a pointer stores the address, the constant pointer stores a constant address. Thus, though the value of a constant pointer is modifiable, the address must remain the same. Eg - int var = 60; int* pv = &var; //Assume &var = 0x12FFD. int const* cpv = &var; // Constant pointer - cpv. var++; // No error. cout<<*cpv; // Prints 61. pv++; // Valid operation. pv = 0x12FFE. cpv++; // Error. Moving to different address. // cpv must stay at 0x12FFD. Note that 'int const*' is different from const int*, as the first is a constant pointer but the second is a pointer pointing to a constant variable, and can have a change in address and not a value.
21st Jan 2018, 4:11 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar