Constants and pointers (pointers and constants)

Source: Internet
Author: User

Constants and pointers ( Pointers and constants ) -- Const-modified pointer confusing generally involves a pointer involved in a constant modified with const, which is easy to confuse with the head. A simple technique is to look from the right to the left, the following is an example: Const int * P1 = NULL ;// Method 1 Int const * P2 = NULL ;// Statement 2 Int * const P3 = NULL ;// If the value assignment statement is commented out and "// error" is added, the error code is displayed. Others are correct: P1 = NULL; P2 = NULL; // * P1 = 1; // Error // * P2 = 2; // Error // P3 = NULL; // Error * P3 = 0; It is equivalent to defining three pointers, P1, P2, P3, and writing one and writing two. Therefore, we mainly look at the differences between P2 and P3. Based on our right-to-left technique, it is to see the right side of const: P2: The right side of const is "* P2", and the defined constant is the constant pointed to by P2, then P2 = 0 is correct, * P2 = 0 error P2: the right side of const is "P3". If the defined constant is the pointer P3 itself, P3 = 0 is incorrect, * P3 = 0 is correct.

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.