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