Const modifier pointer

Source: Internet
Author: User

The const pointer modifier is generally divided into the following four situations:

Int B = 500; const int * A = & B; // 1int const * A = & B; // 2int * const A = & B; // case 3 const int * const A = & B; // case 4

How is the difference?

If the const is on the left side of the asterisk, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant;

If const is on the right side of the asterisk, const modifies the pointer itself, that is, the pointer itself is a constant.

1. In case 1 and 2 are the same, the content pointed to by the pointer is a constant. In this case, the content cannot be changed.

For example

Int B = 500; const int * A = & B; // case 1 * A = 600; // Error

However, there are other ways to change the value of * A. One is to change the value of B, and the other is to point A to a = & C elsewhere.

In case 1 and 2, Initialization is not allowed. Although the pointer content is a constant, the pointer itself is not a constant.

2. Case 3: the pointer itself is a constant, and the pointer itself cannot be changed. The content pointed to by the pointer is not a constant.

Therefore, the definition must be initialized at the same time.

3. Case 4: the pointer itself and the content pointed to are constants.

 

Const modifier pointer

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.