1. Pointer to a constant (pointer to const)
The pointer to const cannot change the value of the variable (object) it points to, or the value of the variable (object) cannot be changed through this pointer to const.
// Pointer to const, the value of the variable (object) pointed to cannot be modified through *cptr
const double pi = 3.14;
const double *cptr = π
// Error, the value of the pointed variable (object) cannot be modified by dereference pointer
*cptr = 42
2. Const pointer (const pointer)
After initialization, the address stored in the const pointer cannot be changed. Or when this const pointer is initialized, it points to a variable (object), and the pointer of the const pointer cannot be changed.
// const pointer
int errNumb = 0;
int newVal = 77;
int *const curErr = &errNumb;
// Error, the pointer cannot be modified
curErr = &newVal;
3. Constant pointer to a constant (const pointer to a const variable)
Const variable referred to by this title ==Const ==Const object
The const pointer to a const variable cannot be changed, whether it is the value of the variable (object) it refers to or the address it stores itself, or the value of the variable (object) cannot be changed through this pointer, and the pointer's pointer cannot be changed after the pointer is initialized.
const double pi = 3.14;
const double newVal = 77.88;
const double *const pip = π
// Error, the pointer cannot be modified
pip = &newVal;
// Error, the value of the pointed variable (object) cannot be modified by dereference pointer
*pip = 7.62;
4. Identify the constant pointer---the pointer's pointer cannot be modified
int errNumb = 0;
int newNumb = 7;
int *const curErr = &errNumb;
const double pi = 3.14;
const double pp = 8.88;
// Constant pointer to constant
const double *const pip = π
// Correct, you can modify the value of the object pointed to
*curErr = 8;
// Error, the pointer's pointer cannot be modified, that is, it cannot be re-pointed to another object after initialization
curErr = &newNumb;
// ----------------------------------------------------------
// Error, the value of the pointing object cannot be modified
*pip = 7.62;
// Error, the pointer's pointer cannot be modified, that is, it cannot point to another object after initialization
pip = &pp;
In the above code,Read from right to leftdefinition:
The closest symbol to curErr is const, which means that curErr itself is a constant object, and then read to the left is a *, which means that curErr is a constant pointer at this time.
From this we can see that pip is also a constant pointer.That is, as long as the*const identifier
Such a pointer combined in sequence is aPointer pointer pointer cannot be modifiedofConstant pointer
5. Identify pointers to constants----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const double pi = 3.14;
const double pp = 7.62;
const double *cptr = π
// Error, the value of the pointing object cannot be modified
*cptr = 42;
// Correct, you can modify the pointer's pointer
cptr = &pp;
In the above code,Read from right to leftdefinition:
The symbol closest to cptr is *, to the left is a data type, which is double, and to the left is const, which means that cptr is a pointer to a constant at this time.
Because cptr is a pointer, it points to a constant pi, so cptr is called a pointer to a constant
That is, as long as it meetsconst data type * identifier
Such a pointer combined in sequence is aThe value pointing to the object cannot be modifiedofPointer to a constant
It is worth mentioning thatPointer to a constantIts pointing can be modified
6. Summary
Pointer to constants: cannot be used to change the value of the variable (object) pointed, but can modify the pointer's pointer
Constant pointer: The pointer's pointer cannot be changed, that is, the address value stored by the pointer itself cannot be changed.
Constant pointer to a constant: It cannot change the value of the variable (object) pointed to by the pointer, nor can it change the pointer's pointer
Tips:
In fact, the pointer itself is a constant does not mean that the value of the object it refers to cannot be modified through the pointer. Whether this can be done depends entirely on the type of the object it refers to.
For example, pip is a constant pointer to a constant, neither the object value pointed to by pip nor the address stored by pip itself cannot be changed
On the contrary, curErr points to a general non-const integer errNumb. Because errNumb is not a constant, you can use curErr to modify the value of errNumb.