1 int I speaking
You know when we declare a variable like this int i; This i is possible to reset the assignment at it. As follows:
int i=0;
...
i=20;//here again.
But one day my program might need such a variable (call it a variable) and assign an initial value when it is declared. After that, my program will not be able to assign it again at any other place. What should I do then? With a const.
//**************
const int IC = 20;
...
ic=40;//this is not possible and cannot be passed at compile time because we cannot reassign the const modified ICS.
This will make our program easier to spot problems earlier.
//**************
With a const-modified IC We don't call it a variable, but a symbolic constant that represents the number 20. This is the role of the Const. The IC is not able to reassign the new value at its place.
After recognizing the role of the const, in addition, we also need to know the format of the wording. There are two kinds: const int ic=20; and int const ic=20;. They are exactly the same. We have to be clear about this. In short, it is important to remember that neither the const nor the int does not affect the semantics before it is written. With this concept, let's look at the two guys: const int * PI and int const * PI, according to your logic, are their semantics different? Oh, you just remember a little, the int and the const which put before which is the same, like the const int IC, and int const IC; In other words, they are the same.
All right, we've got a "double tire" problem now. So what's the difference between the int * Const PI and the first two formulas? I will be the following to analyze their format and semantics bar!
2 The semantics of the const int * pi
Let me first say what the const int * pi is for (of course int const * PI is the same, as we said before, they are actually the same). Look at the following example:
//*************代码开始 ***************
int i1=30;
int i2=40;
const int * pi=&i1;
pi=&i2; //4.注意这里,pi可以在任意时候重新赋值一个新内存地址
i2=80; //5.想想看:这里能用*pi=80;来代替吗?当然不能
printf( “%d”, *pi ) ; //6. 输出是80
//*************代码结束***************
Semantic Analysis:
I see it. The value of pi can be modified. That is, it can point back to another address, but you cannot modify the value of I2 by *pi. Does this rule fit the logic we talked about earlier? Of course it fits!
First of all, the const modifier is the entire *pi (note that I'm writing *pi instead of pi). So *pi is a constant and cannot be assigned (although Pi refers to i2 as a variable, not a constant).
Secondly, Pi is not modified by const, so pi is a pointer variable that can be assigned to another memory address. You may wonder: then how can I use const to modify PI? In fact, you notice that the position of the const in the int * Const PI will probably be understood. Keep in mind that semantics are seen through formatting. Haha, you may have seen the law, right? There is no need to see the next section. But I have to go on with my fight!
3 See int * const PI again
Indeed, the int * Const PI is easily confused with the preceding int const PI. Note: The const in the preceding sentence is written before and after Pi, not in the *PI. Obviously, it is modified to qualify pi. I'll show you the example first:
//*************代码开始 ***************
int i1=30;
int i2=40;
int * const pi=&i1;
//pi=&i2; 4.注意这里,pi不能再这样重新赋值了,即不能再指向另一个新地址。
//所以我已经注释了它。
i1=80; //5.想想看:这里能用*pi=80;来代替吗?可以,这 里可以通过*pi修改i1的值。
//请自行与前面一个例子比较。
printf( “% d”, *pi ) ; //6.输出是80
//***************代码结束 *********************
Semantic Analysis:
Look at this code, what do you understand? There was no value in the PI value that could be modified. It can always point to the memory address that was initialized. Instead, you can modify the I1 value by *pi this time. Compare it to the previous example! Look at the following two points analysis
1 pi because of the modification of the const, it is just a pointer constant: that is, the PI value is not modifiable (that is, PI can not be pointed back to the i2 variable) (see line 4th).
2 There is no const modification to the front of the whole *pi. That is, *PI is a variable rather than a constant, so we can modify the value of the memory I1 it refers to by *PI (see 5 lines of comments)