当前位置:网站首页>Detailed explanation of pointer constant and constant pointer
Detailed explanation of pointer constant and constant pointer
2022-07-27 05:09:00 【Meme_ xp】
title : Pointer constants and constant pointers
1. Constant pointer :
int a,b;
const int *p1=&a;
int const *p2=&b;
1.1 Memory method :
const stay *p Let's read out const( Constant ) Then read the pointer , Together, they are constant pointers .
1.2 understand :
We can understand it as a " Constant " The pointer to , There is no rule on which one to point to " Constant ", So the direction of the pointer can be changed . go back to " Constant " The word ," Constant " It's an unchangeable quantity, so we You can't go through the pointer To modify this " Constant " Value . The constant here is quoted , Next, we will explain clearly in the notes .
1.3 matters needing attention :
Constant pointers cannot be modified by pointers a Value , But we can modify it directly a Value .
1.3.1 The first way to write it :
int a=10;
const int *p=&a;
a=20;
cout<<"a="<<a<<","<<"*p="<<*p;
There is no mistake in this way , I'll explain later .
1.3.2 The second way :
int a=10;
const int *p=&a;
*p=20
cout<<"a="<<a<<","<<"*p="<<*p;
The compiler will report an error for the above writing :[Error] assignment of read-only location ‘* p’. Let's translate this sentence , Roughly meaning :*p Allocated in the read-only area . Therefore, writing , Why do you explain it like this ? Is it in conflict with the above understanding . Does not , Because we can understand that :a Itself is a variable , It's just that we started with const int *p, Look in this direction , I just put a As a constant , This is how I look at compilers in this direction . But in fact a Itself is a variable and cannot pass * p Can also be changed in other ways . For example :
float num=3.14;
cout<<"num="<<(int)num;
A simple example ,num It's obviously a float, But we just want to treat him as a int.
2. constant pointer :
int c;
int * const p3 =&c;
2.1 Memory method :
You can see from the expression that int * stay const front ,int* Can represent pointer , and const For constant , Together, we read it as a pointer constant .
2.2 understand :
We know that constants are immutable quantities . From the literal meaning, we can see that constants modify the two words pointer . So we can't change the value of the pointer , The value of the pointer is another address , Push it, we can't point to the pointer ( That's the value of the pointer ). But you can modify variables c Value , Because there is no regulation c Is a constant .
3.
Of course const int * const p That is, the pointer cannot be changed and the value of the variable cannot be modified through the pointer . But it can still be accessed directly through a To modify the a Value . For explanation, see 1.3
int a=10;
const int * const p=&a;
a=20;
cout<<"a="<<a<<","<<"*p="<<*p;
4. summary :
This article explains my simple connection between pointer and Chang from my personal point of view , If you have any questions, please leave a message in the comment area or send a private message to me . Views for reference , If there are mistakes, you are welcome to correct .
边栏推荐
- R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- 文件处理(IO)
- Acceptance and neglect of events
- [search] - multi source BFS + minimum step model
- 老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
- Installation and template setting of integrated development environment pychar
- 接口和抽象类/方法学习 demo
- MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
- Sunset red warm tone tinting filter LUTS preset sunset LUTS 1
猜你喜欢
随机推荐
抽卡程序模拟
智慧展厅设计的优势及适用行业分析
feign调用丢失请求头问题解决及原理分析
Invert a Binary Tree
Three paradigms, constraints, some keyword differences,
Dialog introduction
Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images
How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
2022 T2i text generated image Chinese Journal Paper quick view-1 (ecagan: text generated image method based on channel attention mechanism +cae-gan: text generated image technology based on transforme
Solution: read the files with different names in the two folders and deal with the files with different mappings
DBUtils
Use of file i/o in C
Typescript details
Counting Nodes in a Binary Search Tree
Introduction to Kali system ARP (network disconnection sniffing password packet capturing)
Another skill is to earn 30000 yuan a month+
接口和抽象类/方法学习 demo
Counting Nodes in a Binary Search Tree
Web框架介绍
文件处理(IO)









