Bjarne provides a mnemonic method in his c ++ programming language:
Read a declaration from right to left:
Char * const CP; CP is a const pointer to Char;
Const char * P; P is a pointer to const char;
Char const * P; Same as above (because there is no const * operator in C ++, const can only belong to the previous type );
Conclusion:
Char * const CP: defines a const pointer to a character, that isPointer constant;
Const char * P: defines a pointer to a character constant, that isConstant pointer;
Char const * P: equivalent to const char * P
Pointer constant
A constant of the pointer type, that is, the pointer itself is a constant. The address to which the Pointer Points cannot be changed, but the content corresponding to the address to which it points can be changed.
Pointer constant Declaration: type * const pointer;
CP = P // error because it is a pointer constant and changes the pointing address
* CP = 'A' // correct because the content of the address to which it is directed can be changed
Constant pointer
A pointer that points to a constant. The content to which the Pointer Points cannot be changed, but the address to which it points can be changed.
Constant pointer Declaration: const type * pointer/type const * pointer;
CP = P // correct. Because this is a constant pointer, you can change the pointing address.
* CP = 'A' // error because the content of the address cannot be changed