Const modifier
When you use the const modifier variable, the value of the variable is not modifiable and therefore needs to be initialized.
For example const int s = 0;
At this point s is the variable with the value immutable.
So based on this, there are three things when the const modifier pointer is:
(1) const int *p;
The const modifier is *p instead of p, so the value of *p is immutable, and the value of p is variable, so p is not initialized.
For example
int s = 0,t = 1;
const int *p;
p = &s;
cout<<*p<<endl;
p = &t;
cout<<*p<<endl;
The above code output:
0
1
But at this point, no matter p points to s or T, you cannot modify the value of the variable p points to by *p, such as *p = 2; (This is wrong because Const modifies the *p, and *p is immutable at this time)
(2) int *const p = &s;
At this point the position of the * is in front of the const, and the const directly modifies p, so the point of P is fixed and immutable, so it must be initialized.
At this point, however, you can still modify the value of the variable that p points to by *p.
For example, int s = 0;
int * Const P = &s;
cout<<*p<<endl;
*p = 3;
cout<<*p<<endl;
The above code output is:
0
3
(3) const int *const p = &s
In this case, both P and *p are immutable, so initialization must be required.
The third situation is actually the combination of the first two cases, it is not specifically analyzed.
Const and pointers in C + +