1. const int *ptr = NULL; <=> int const *PTR = NULL;
1) Represents a pointer variable to a symbolic constant, and the pointer variable itself is not const so it can point to other variables.
2) The role of Const can be seen as "forbidden by *ptr" To change the value of the variable that the PTR points to, but the variable being pointed to can change its own definition .
eg:const int i = 1, int j = 2;
PTR = &i; Ok
*ptr = +//Error
i = 2//Error i is const
PTR = &j; Ok
*ptr = 28; Error
j = 3; Ok
2. const int *PTR vs int * const PTR;
1) to distinguish between the above two forms of emphasis on the relative position of the const and *:
Const indicates "pointer to symbol constant" before *
Const represents "a constant pointer to a variable of type int" after *
Note: if declared as int * Const PTR = NULL, the value of PTR cannot be changed to point to another variable.
However, you can modify the variable that it points to (if it points to it) by *ptr.
2) Memorizing skills
See if the const modifier is *ptr or ptr?
When const can be assumed to be a const modifier before * *ptr, the value of *ptr is immutable and the value of PTR is variable
When const is assumed to be a const-modified PTR after *, the value of PTR is immutable and the value of *PTR is variable
Const modifies who, whose value cannot be changed.
3. const int * const PTR;
1) indicates "constant pointer to int type symbol constant"
2) const modifies the *ptr to indicate that the value of the pointer to the variable cannot be modified by *ptr
It also modifies the PTR to indicate that the value in the PTR can no longer be modified to point to another variable
Const *ptr PTR