I have been using C++ recently, and pointers really make me bald, and seeing the combination of const and pointers makes me want to die.
int a = 10;
int b = 20;
const int * p = &a;
*p = 20; //错误
//更改p指向的地址
p = &b;
1. const int * p and int const * p
The value pointing to the address cannot be changed by p, but can point to other addresses.
2、 int *const p
This is a constant pointer. Once bound, the address pointed to cannot be changed, but the value in the pointed address can be changed by dereferencing.
int a = 10;
int b = 20;
int * const p = &a;
*p = 30;
p = &b; //错误
3. const int * const does not allow changing the pointed address and changing the pointed value through p
int a = 10;
int b = 20;
const int * const p = &a;
p = &b; //错误
*p = 20; //错误