Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between const, pointer and reference in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article, "what is the relationship between const, pointers and references in C language?", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the relationship between pointers and references in C language".

Const and pointer

Let's write a piece of code to explore the following

Int a = 10, b = 20 intt * p1 = & an intt * p1 = 100 p1 = & Bten Const int* p2 = & a tint const* p3 = & an intt * const p4 = & a

Above, const int* p2; is equivalent to int const* p3 = & a;

Const is to the left of the asterisk, and our const modifies the pointing ability, that is, we can change the self value of p2, but we cannot change the value of * p2.

Int x = * p2

* p2 = 100; / / err is incorrect

P2 = & b; / / is correct

Const is to the right of the asterisk. We cannot change the value of p4, but we can change the value of * p4.

Int x = * p4

* p4 = 100; / / is correct

P4 = & b; / / err is wrong

And p5 can neither change its own value nor change the value of * p5.

Let's now look at the following code to explore which sentence is right and which is wrong.

Int a = 10 int b = 20 Const int * p = & a; * p cannot be changed int * s 0 = p so that * p can be changed const int * s 1 = p Ting const s 2 can not be changed const int * const S3 = p x p cannot be changed S3 cannot be changed

When we analyze const int* p = & a;, we can see that the const modification makes * p unmodifiable, and then analyze whether the following code will modify * p.

First of all, we can see that our S0s1s2s3 is equal to p and then * S1 * S2 * S3 * p is a. We mentioned earlier that capabilities can be contracted but not expanded.

1.s0 is equivalent to p, and we can modify * s0, which is wrong.

2.const modifies * S1, so we can't modify * S1, we can modify S1 and then correct

3.cosnt modifies S2, we can modify * S2, but not S2, so it is wrong

4. We can modify neither S4 nor * S4, so it is correct.

Let's take a look at a piece of code like this, this time we're going to const p.

Int a = 10 int * const p = & a; p cannot change int * S0 = p; S0 can be changed * S0 = = * p can be changed const int * S1 = p; * S1 cannot be changed S1 can be changed int * const S2 = p; S2 cannot be changed * S2 can be changed const int * const S3 = p; S3 cannot be changed * S3 cannot be changed

We analyze int* const p = & a;, this time our const modifies p so that the value of p cannot be modified, but * p can be modified, and then analyze the following code

First of all, as above, s0s1s2s3 is equal to p and then s1s2s3p is a

1.s0 is equal to p, we can modify * p, we can modify s0, but there is no change to p, so it is correct.

2.const modifies * S1, we can't modify * S1, we can modify S1, it has nothing to do with p, so it's correct.

3.const modifies S2, we can't modify S2, we can modify * S2, it has nothing to do with p, so it's correct.

4. We can neither modify S3 nor * S3 here, which has nothing to do with p, so it is correct.

If we change the value through a constant address by casting

Int main () {const int a = 10; int b = 0; int* p = (int*) & a; * p = 100; b = a; cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report