Const int * P, const * int P, int const * P

Source: Internet
Author: User

1 start with const int I
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 const int * pi. See the following example:
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.

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:
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, * PI rather than Pi cannot be changed.
2) If const is directly written before Pi, the PI cannot be changed.

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
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.

Case 2: When the const int * PI Pointer Points to const int I1
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.

Case 3: pointer declared with const int * const pi
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.

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.