Often explain pointer type (const int * p, int * const p, int const * p, pointer constant, constant pointer)

1 , base type: const int n-  :

const int n = 20, n modified with const variables we do not call it, and said the symbolic constants, representing 20 this number. This is the const role. n can not be re-assign a new value in another location.

Format written: const int n = 20; and int const n = 20; they are identical. const int and which do not affect the semantics before writing.

With this in mind, we look at these two formats: const int * PI and int const * PI  , remember one thing, int const which the previous release which after release are the same, that is to say, they are the same of.

2 , const int * PI semantics

My first is that const int * pi is what role. See the examples below:

 

int n1 = 30;

int n2 = 40;

const int * pi = &n1;

pi = & n2; // note here, pi can reassign a new memory address at any time

n2 = 80; // Think about it: Here can * pi = 80; instead? Of course not

the printf ( "% D ", * PI); // Output 80

 

Semantic Analysis:

The value of pi can be modified. I.e. it can be re-directed to another address, but can not be modified * pi value of n2. This rule is in line with our stated before the logic of it? Of course, in line!

First const * pi modified whole (note that I wrote * pi rather than pi). So * pi is a constant, can not be assigned (n2 although within the meaning of pi is a variable, not a constant).

Secondly, before pi did not use const modified, so pi is a pointer variable, the assignment can be redirected to another memory address.

3 , look int * const pi

Indeed, int * const pi previous int const * pi will be very easy to confuse. Note: The preceding sentence is written in the const before and after the * pi instead of pi * written before. Obviously, it is a modified limited pi. Look at the example:

 

int n1 = 30;

int n2 = 40;

int * const pi = &n1;

// pi = & n2;             note here, pi could not afford to re-assignment, and that is no longer points to another new address.

n2 = 80; // here can * pi = 80; instead? Can, here * pi by modifying n1 values.

// compare with the previous example of a self.

the printf ( "% D ", * PI); // Output 80

 

Semantic Analysis:

Here is the value of pi can not be reassigned modified. It can always point to the memory address when initialized. Instead, you can modify the value of n1 through * pi. Comparison with the previous example, see the following two points analysis

1). Pi because of modified const, so it is a pointer constant, which means that the value of pi can not be modified (ie, pi can not redirect the variables n2).

2) No modification const * const pi in front of the whole. In other words, * pi is a variable rather than constant, so we can modify it by referring to the value of memory n1 * pi.

In a word, this pi is a pointer to a constant int variable types of data.

4 , summarized:

One,

1) If const * pi modification before it can not be changed * pi and not to pi.

2) If const is written directly in front of the pi pi can not be changed.

Second, often the pointer actually only three forms:

1, const int * pi constant pointer.

2, int * const pi pointer itself is a constant, it referred to as "pointer constant", "Chang pointer."

3, const int * const pi constant pointer constant.

2 and 3 form must be assigned at the time of declaration.

 

                                        Refer to the original address: http: //blog.csdn.net/yjh0628/article/details/5830153

Guess you like

Origin www.cnblogs.com/xyb617/p/10984661.html