(1) Example: 1. Int I = 1;
Const int * cptr = & I;
* Cptr = 2; // error. cptr points to constant 1.
Cout <* cptr <Endl ;//
2. Int I = 1;
Const int * cptr = & I;
I = 2; // normal
Cout <* cptr <Endl; // output 2
3. Int I = 1, TT = 8;
Const int * cptr = & I;
Cptr = & tt; // normal
Cout <* cptr <Endl; // output 8
To sum up, the only result caused by const pointer is that * cptr =... cannot be used, and others can be used (cptr itself can be changed, but its point cannot be changed)
But above Program It doesn't make sense. The const Pointer Points to the const int...
Const int A = 1;
Const int * cptr = & A or const void * cptr = & A; // constant, the pointer must also be a constant
Note: const pointers are commonly used as function parameters. Define the form parameter as a pointer to const to ensure that the actual object passed to the function is not modified because of the form parameter in the function.
(2) The value of const pointer itself cannot be changed, but whether it points to can be changed based on whether the object to be pointed to is const.
Int I = 10, TT = 88; // an error occurs if it is const int I = 10.
Int * const cptr = & I;
* Cptr = 9; // Yes
Cptr = & tt; // Error
Cout <* cptr <Endl;
(3) The const pointer to a const Object Pointer itself and the object to which it points cannot be changed.
Const int I = 10;
Const int * const cptr = & I;
conclusion: If const is added, it is displayed from the right to the left, that is, the right to the left.
for example, const int * cptr is an int pointer, which is a constant
int * const cptr (pointer) is const type. This const points to int
const int * const cptr = & I; cptr is const type, while 2nd const points to int type, and this int is a constant
Original variable: int type --> pointer variable int * const P, const int *, const int * const Pi = &;
Const int --> pointer variable const int * P, const int * const Pi = &;
Additional:
1 const string STR;
String const STR; // The same as the two
2 string S;
Typedef string * pstring;
Const pstring cstr1 = & S; // 1 Type
Pstring const cstr2 = & S; // two types of all three decreations are the same type
String * const cstr3 = & S; // they're all const pointers to string
)