Const char *, char const *, char * const difference; pointer constant, constant pointer difference

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.