In the previous article, the array name is a constant pointer.
Reference: http://blog.pfan.cn/whyhappy/5164.html
Const int * pi, int const * pi and int * const pi and their operations
1 start with const int I
You know, when declaring a variable, int I is like this; this I may be re-assigned to it. As follows:
Int I = 0;
//...
I = 20; // The value is assigned again here.
However, one day my program may need such a variable (temporarily called its variable) and assign an initial value when declaring it. Then, my program will not assign a value to it in any other place. What should I do? Use const.
//**************
Const int ic = 20;
//...
Ic = 40; // This is not acceptable. It cannot be passed during compilation, because we cannot re-assign values to the const modified ic.
// In this way, our program will be more likely to discover problems earlier.
//**************
With the const modified ic, we do not call it a variable, but a symbolic constant, representing the number of 20. This is the role of const. An ic cannot be re-assigned to it.
After understanding the role of const, we also need to know the format. There are two types: const int ic = 20; and int const ic = 20 ;. They are identical. We need to be clear about this. In short, you must remember the meaning of const and int before writing. With this concept, let's look at the two guys: const int * pi and int const * pi. According to your logic, are their meanings different? Well, you just need to remember that int and const are the same before and after the put, just like const int ic; and int const ic. That is to say, they are the same.
Now we have fixed the problem of "dual-pack. So what is the difference between int * const pi and the first two formulas? Let me analyze their formats and semantics in detail!
2 const int * pi semantics
Let me first talk about the role of the const int * pi (of course, the int const * pi is the same, as we have said before, they are actually the same ). See the following example:
// ************** Code start ***************
Int i1 = 30;
Int i2 = 40;
Const int * pi = & i1;
Pi = & i2; // 4. Note that pi can assign a new memory address at any time.
I2 = 80; // 5. Think about it: can I use * pi = 80; instead? Of course not.
Printf ("% d", * pi); // 6. The output is 80
***************
Semantic analysis:
No. The pi value can be modified. That is, it can point to another address again, but it cannot use * pi to modify the i2 value. Does this rule comply with the logic we mentioned earlier? Of course!
First, const modifies the entire * pi (note that I am writing * pi instead of pi ). So * pi is a constant and cannot be assigned a value (although pi refers to i2 as a variable, not a constant ).
Second, pi is not modified with const, so pi is a pointer variable and can be assigned to another memory address. You may wonder: how can I use const to modify pi? In fact, you can understand the position of the const in the int * const pi. Remember to view the semantics through the format. Haha, you may have seen the rule, right? Then there is no need to read the following section. But I have to continue my battle!
3. Look at int * const pi.
Indeed, int * const pi and the previous int const * pi are easy to confuse. Note: the const in the previous sentence is written before and after pi, not before * pi. Obviously, it modifies and limits pi. Let me show you the example first:
// ************** Code start ***************
Int i1 = 30;
Int i2 = 40;
Int * const pi = & i1;
// Pi = & i2; 4. Note that here, pi cannot be assigned again, that is, it cannot point to another address.
// So I have commented on it.
I1 = 80; // 5. Think about it: can I use * pi = 80; instead? Yes. Here, you can modify the i1 value through * pi.
// Compare it with the previous example.
Printf ("% d", * pi); // 6. The output is 80
*********************
Semantic analysis:
After reading this code, what do you understand? Is it found that the pi value cannot be re-assigned and modified. It can only always point to the memory address during initialization. Instead, you can use * pi to modify the i1 value this time. Compare it with the previous example! See the following two points for analysis:
1 ). pi has the const modifier, so it is just a pointer constant: that is, the pi value cannot be modified (that is, pi cannot point to the i2 variable again) (See Row 4th ).
2). There is no const modification before the entire * pi. That is to say, * pi is a variable rather than a constant, so we can use * pi to modify the value of memory i1 (see the comments of five rows)
In a word, this pi is a pointer constant pointing to the int variable type data.
I will summarize the following two sentences:
1). If const is modified before * pi, it cannot be changed to * pi (that is, it cannot be like this: * pi = 50; value assignment) instead of pi.
2). If const is directly written before pi, the pi cannot be changed (that is, it cannot be like this: pi = & I; assignment ).
Please remember these two points first. I believe you will not be confused by them. Now, when I look at the two declarative statements int const * pi and int * const pi, will you feel dizzy or relaxed? What can be modified and cannot be modified by their respective declared pi? Ask yourself again and tell me your understanding. You can post or send it to my mailbox (my mailbox [email protected])! I will definitely reply.
3. Add three cases.
Here, I will add the following three cases. In fact, as long as the above semantics is clear, these three situations have already been included. But as three specific forms, I 'd like to mention it briefly!
Case 1: int * When the pi pointer points to the const int I constant
// *********** Begin *****************
Const int i1 = 40;
Int * pi;
Pi = & i1; // Can this be done? No. It is a compilation error in VC.
// The i1 address of the const int type cannot be assigned to the pointer pi pointing to the int type. Otherwise, won't pi be able to modify the i1 value!
Pi = (int *) & i1; // Can this be done? Forced type conversion is supported by C.
// The i1 value is compiled in VC, but still cannot be modified by * pi = 80. Try it! Let's see how it works.
// ************ End ***************
Case 2: When the const int * pi pointer points to const int i1
// ********** Begin ****************
Const int i1 = 40;
Const int * pi;
Pi = & i1; // The two types are the same. You can assign values like this. Obviously, neither pi nor i1 can be modified.
// ********** End *****************
Case 3: pointer declared with const int * const pi
// ************ Begin ****************
Int I
Const int * const pi = & I; // Can you imagine what operations pi can perform? The pi value cannot be changed or the I value cannot be changed through pi. Because both * pi and pi are const.
// ************* End ****************
References:
Http://blog.pfan.cn/whyhappy/5163.html
Difference between const int * pi/int * const pi