Const keyword, const

Source: Internet
Author: User

Const keyword, const

  • Problem proposal

Analyze the following definitions:

const char* p1;char const* p2;char* const p3;const char** p4;char const** p5;char* const* p6;char** const p7;

 

There are no problems with the first three in 90%. Many people will be confused about the following several estimates. I have checked some materials (including online documents and books ), however, I personally feel that the role of those materials on the const, the const pointer and the pointer to the const value are slightly lacking and slightly messy. Therefore, I wrote this note for the purpose of throwing bricks and jade. You are welcome to give your advice and discuss it.

 

  • Role of const

The role of the const keyword can be summarized as: the first keyword on the rightSymbolLimited to constant. The symbol here is defined:Data Type [*]Or[*] Variable nameOr*(Square brackets indicate optional ). When defining (declaring) a variable, if the variable name is modified to constant, you must assign a value when defining (declaring) the variable. According to the above theory, p1 ~ P7 is the variable name. In char * const p3, const modifies p3, and p3 must be assigned a value when defined; In const char ** p4, const modifies char *, * p4 points to const char *, * p4 does not need to be assigned a value when it is defined. The others are the same.

 

  • Legitimacy of Values

The second penultimate section on page 19th of Expert C Programming said:

To make the assignment form legal, either of the following conditions must be met: both operands are pointers to compatible types with or without delimiters, the Type pointed to by the Left pointer must have all the qualifiers of the type pointed to by the right pointer.

That is to say, when assigning values, either the two sides have the same limitations and are compatible types, or the left side has more limitations than the right side. Based on the functions of the previous const, we know thatConst char **AndChar **Is an incompatible type and cannot be assigned values to each other.

Related Article

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.