In C + +, the const modifier pointer has the following three cases
(1) Pointer constant: A pointer to a constant
const int *P or int const *P
Const in the * before,, can understand its function, because const in the * before, so P point to this int variable for *p is const, that is, cannot change the value of the variable by *p, but the variable itself can arbitrarily change its value. Alternatively, you can change the direction of P.
Cases:
int x=2;
int y=3;
const INT *p=&x;
*p=4; Error, cannot change the value of a variable by *p
x=4; Yes, x is not a constant, you can change your own value
p=&y; Yes, the pointer p itself is not a constant and can change the direction of P.
(2) Constant pointer:
int * Const P,CONST at the back of *, that is, the pointer p itself is a constant, can not change the direction of P. However, you can change the value of the pointed variable by *p.
Cases:
int x=2;
int y=3;
int * Const p=&x; Note: Because pointer P is a constant, it should be initialized at the time of declaration.
*p=4; OK
x=4; Yes, x is not a constant, you can change your own value
p=&y; Error, the pointer q itself is a constant and cannot change its point.
(3) constant pointer to constant: this is the combination of the first two, the function is the combination of the former, that is, can not change the direction of P, nor through the *p to change the value of the variable pointed to.
const INT * const p or int const *P
Cases:
int x=2;
int y=3;
const INT * Const pq=&x;
*pq=4; No, it is not possible to change the meaning of the indicated variable by *PQ.
x=4; Yes, x is not a constant, you can change your own value
p=&y; Error, the pointer PQ itself is a constant and cannot change its point.
Summary: As can be seen from the above, in either case, the variable itself can change its own value, unless the variable itself is declared as a constant (const int x=2;). The key to distinguish between three kinds of situations is to see if the const is before the type or before the pointer, before the type, cannot change the value of the variable by *p, cannot change the pointer's point before the pointer, and initializes it at the time of declaration.
The const and pointer combinations in C + +