Const pointer and pointer to const

Source: Internet
Author: User

Const pointer and pointer to const

There are two meanings when using a pointer with Const. One means that you cannot modify the content of the pointer, And the other means that you cannot modify the content pointed to by the pointer. It sounds a bit confusing. Let's give an example to understand it later.

First, the pointer to const indicates that the content pointed to by the pointer cannot be modified. It can be written in two ways.

Const int * P; (recommended)

Int const * P;

The first one can be understood as P is a pointer that points to the const int type. P itself can point to any identifier without initialization, but the content it points to cannot be changed.

The second one is easily understood as P is a const pointer pointing to int (the pointer itself cannot be modified), but this understanding is wrong, and it also indicatesIs a pointer to const(The Pointer Points to content that cannot be modified. To avoid confusion, we recommend that you use the first method.

BesidesConst pointerIt means that the value of the pointer itself cannot be modified. It has only one way to write

Int * const P = an address; (because the value of the pointer itself cannot be modified, it must be initialized)

This form can be understood as P is a pointer, which is a const pointer to the int. It points to a value that can be changed, such as * P = 3;

Another case is that the pointer itself and the content it points to cannot be changed. Please refer to it.

Const int * const P = an address;

Int const * const P = an address;

This rule is: the pointer to const (the content pointed to by the pointer cannot be modified). The const Guan Jian word always appears on the left *.,The const pointer (the pointer itself cannot be modified) is always on the right side, needless to say, adding a * in the middle of two const must be that the pointer itself and the content it points to cannot be changed.

/* Define the pointer to const (the content pointed to by the pointer cannot be modified )*/

Const int * P1;

Int const * P2;

/* Define the const pointer (Initialization is required because the value of the pointer itself cannot be changed )*/

Int * const P3 = &;

/* The pointer itself and the content it points to cannot be changed, so Initialization is also required */

Const int * const P4 = &;

Int const * const P5 = & B;

Original article:

Http://www.cnblogs.com/younes/archive/2009/12/02/1615348.html

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.