Constant pointer and pointer constants

Source: Internet
Author: User

A constant pointer, a center of gravity point at the back, it is a pointer, it points to a constant. The content of the address it points to cannot be modified to prevent errors such as modifying constants that occur when the pointer is mistakenly manipulated.

The pointer constant, the center of Gravity Point in the back, it must first be a constant, and then it is a pointer. The system cannot modify the address pointed to by the pointer constant, which means that the address pointed to by the pointer is fixed once it is initialized. However, the contents of the address it points to can be changed.


(1) char* const P1;

(2) Char const* p2;

(3) const char* p3;

(4) const char* const P4;

Analysis of the above four statements: in fact, only need to see the const distance which is near, which is to modify which.

(1), the const is used to decorate the P1 pointer itself, that is, the pointer itself is a constant, but the P1 point can be modified;

(2), the const is used to modify the pointer pointing to the variable, that is, the pointer p2 the content pointed to must be constant, cannot be modified;

(3) the same as (2);

(4), the content pointed to by the previous const modifier pointer, the next const-decorated pointer itself, that is, the pointer P4 itself is a constant, and the content it points to must be constant, cannot be modified.


Therefore, p1 is a pointer constant;

P2,P3 is a constant pointer;

The P4 both accounted for.

Constant pointer and pointer constants

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.