C ++, a troublesome const keyword

Source: Internet
Author: User

The complex statements of C and C ++ follow the right-left rule.
This rule helps you quickly determine the exact meaning of a complex statement.

 

/* <Br/> const and pointer <br/> are two headaches for countless C ++ beginners. <Br/> today, this method is simple: Start from the right, for example, if <br/> const char * const A <br/> is combined from right to left <br/> const is encountered, it indicates that a is a constant <br/> *, description A is a constant pointer <br/> char description A points to a character <br/> const description const points to a character constant <br/> thus: A is a constant pointer to a character constant. </P> <p> the following definition helps you understand this method. <Br/> char a ('V'); <br/> char * B (& A); <br/> const char C ('C '); <br/> const char * D; <br/> char * const E (& A); <br/> const char * const F (& C ); <br/> char ** g; <br/> const char ** h; <br/> char * const * I; <br/> const char * const J = ??; <Br/> Can you tell us what they represent? <Br/> */</P> <p> # include <iostream> <br/> using namespace STD; <br/> int main () <br/>{< br/> char a ('V'); // character variable </P> <p> char * B (& ); // character pointer </P> <p> const char C ('C'); // character constant </P> <p> const char * D; // pointer variable pointing to a character constant </P> <p> char * const E (& ); // a constant pointer to a character variable </P> <p> const char * const F (& C ); // a constant pointer to a character constant </P> <p> char ** g; // a pointer variable, it points to a pointer to a character variable </P> <p> const char ** h; // a pointer variable, it points to a pointer to a character constant </P> <p> char * const * I = ??; <Br/> const char * const J = ??; // Leave the two for your consideration </P> <p> return 0; <br/>}
 

 

 

 

 

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.