String pointer and character pointer const

Source: Internet
Author: User

The advantage of using const is that it allows you to specify a semantic constraint -- an object cannot be modified -- the compiler implements this constraint. With const, You can notify the compiler and otherProgramA member value must remain unchanged. As long as this is the case, you need to use const explicitly, because this can be done with the help of the compiler to ensure that such constraints are not broken.

The const keyword is truly fascinating. Outside the class, it can be used for global or namespace constants and static objects (local objects within a certain file or block range ). Inside the class, it can be used for static and non-static members.

For pointers, you can specify the pointer itself as the const, or specify the data referred to by the pointer as the const, or both as the const, and neither of them as the const:

Char * P = "hello"; // non-const pointer,
// Non-const data

Const char * P = "hello"; // non-const pointer,
// Const data

Char * const P = "hello"; // const pointer,
// Non-const data

Const char * const P = "hello"; // const pointer,
// Const data

The syntax does not seem so changeable. In general, you can draw a vertical line in your mind through the asterisk (*) position in the pointer declaration. If the const appears online on the left, the pointer points to a constant; if const appears on the right side of the line, the pointer itself is a constant. If const appears on both sides of the line, both are constants.

When the pointer refers to a constant, some programmers like to put the const before the type name, while some programmers like to put the const after the type name and before the asterisk. Therefore, the following functions take the same parameter type:

Class widget {...};

Void F1 (const widget * PW); // F1 is directed
// Widget constant object pointer

Void F2 (widget const * PW); // same as F2

Because the two forms areCodeSo you have to get used to both forms.

Some of the powerful functions of const are based on its application in function declaration. In a function declaration, const can refer to the return value of a function or a parameter. For a member function, it can also refer to the entire function.
References: http://hi.baidu.com/freeemperor/blog/item/19d97606285c8278030881fa.html

 

Note: const char * P is a character constant pointer, but there are some restrictions, that is, the pointer cannot be changed, and the pointer can be changed. For example, if p = & A is used, P cannot be modified. If P = & B is used, an error occurs.

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.