We can refer to the practice on item21 in Objective C ++. If const is on the left side of the asterisk, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant; if const is on the right side of the asterisk, const modifies the pointer itself, that is, the pointer itself is a constant.
For example:
# Include <iostream>
Using namespace STD;
Int main ()
{
Int A = 3, B = 10;
Int * const Pa = &;
Const int * CPA = &;
// Pa = & B; // Error
CPA = & B;
* PA = 5;
// * CPA = 5; // Error
Return 0;
}
Is it easy?
In fact, even so, I think everyone still needs to memorize it, isn't it? It's very troublesome. Maybe I forget it one day... I can teach you how to learn this experience:
In fact, you only need to remember that the constant string is of the const char * type.
For example:
Const char * P = "xinfanyiluan ";
P = "xinfanyiluan2 ";
// * P = 'D'; // error. The constant cannot be assigned.
Because it is a constant string, its value cannot be changed naturally, right?
Very good, so only one const is displayed, isn't it? It's still on the left of *, isn't it?
Now, you can associate const char * with a constant string:
The pointer is variable, but its pointing value is immutable.
The other case is definitely the opposite, isn't it?
I don't know if you think you don't need to memorize it after learning it like this?