Const int * P, int * const P, and INT const * P

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/yjh0628/article/details/5830153

1 starting from const int n
Const int n = 20, with the const modified N
We do not call it a variable, but a symbolic constant, which represents the number of 20. This is the role of Const. N is not allowed to re-assign new values in it.
Met const
Then, we need to know the format. There are two types: const int n = 20; and INT const
N = 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 are different in their semantics according to your logic? You just need to remember that int and const
It is the same as the one before and after the put, just like the const int N; same as the int const N. That is to say, they are the same.
2 const int *
Pi Semantics
Let me first talk about the role of const int * pi. See the following example:
Int n1 = 30;
Int n2 = 40;

Const int * Pi = & N1;
Pi = & N2; // note that pi can assign a new memory address at any time

N2 = 80; // think about it: Can I use * Pi = 80; instead? Of course not.
Printf ("% d", * PI); // 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 N2 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 ). Therefore, * pi is a constant and cannot be assigned a value (although N2 is a variable rather than 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. Check int * const again.
Pi
Indeed, int * const PI and the previous int const * Pi are easy to confuse. Note: The const in the previous sentence
It is written before PI and after *, not before * pi. Obviously, it modifies and limits pi. Let me show you the example first:
Int n1 = 30;
Int n2 = 40;

Int * const Pi = & N1;
// Pi = & N2;
Note that here, pi can no longer be re-assigned, that is, it can no longer point to another new address.
N2 = 80;
// Can I use * Pi = 80; instead? Yes. Here, you can use * pi to modify the N1 value.
// Compare it with the previous example.

Printf ("% d", * PI); // The output is 80
Semantic Analysis:

Read this sectionCodeWhat 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 N1 value this time. Compare it with the previous example! See the following two points for analysis:

1) Because Pi has the const modification, it is just a pointer constant: that is, the PI value cannot be modified (that is, PI cannot point to the N2 variable again ).

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 memory N1 value.

In a word, this pi is a pointer constant pointing to the int variable type data.
The last two sentences are summarized:
1). If const
* PI cannot be modified before * Pi, not Pi.
2) If const is directly written before Pi, the PI cannot be changed.
4. 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 n constant
Const int n1 = 40;
Int * PI;

Pi = & N1; // Can this be true? No. It is a compilation error in VC. The const int type N1 address cannot
// Assign a value to the int
Type address pointer pi. Otherwise, won't pi be able to modify the N1 value!
Pi = (int *) & N1; // Can this be true? Forced type conversion is supported by C.

// Compiled in VC, but the N1 value cannot be modified through * Pi = 80. Try it! Let's see how it works.
Case 2: const int *
When the PI Pointer Points to const int N1
Const int n1 = 40;
Const int * PI;

Pi = & N1; // The two types are the same. You can assign values like this. The N1 value cannot be modified either through PI or N1.
Case 3: Use const int * const
Pointer of PI statement
Int N;
Const int * const Pi = & N;

// Can you imagine what operations pi can perform? The pI value cannot be changed or the value of N cannot be changed through pi. Because no matter
// * 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.