当前位置:网站首页>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 .
边栏推荐
- 项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
- "Photoshop2021 tutorial" adjust the picture to different aspect ratio
- strlen和sizeof的区别
- Network protocol details: IP
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- Sub database and sub table
- 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
- Invert a Binary Tree
- Approval of meeting OA
- Explanation of index failure principle and its common situations
猜你喜欢

TypeScript 详解

MySQL storage engine and its differences

"Photoshop2021 introductory tutorial" flatten "perspective images

Introduction to Web Framework
![[error reporting] cannot read property 'parsecomponent' of undefined](/img/54/8d4225ec596d6b78348b181a3e636f.png)
[error reporting] cannot read property 'parsecomponent' of undefined
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

Standard dialog qmessagebox

Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?

Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images

Dialog introduction
随机推荐
Web框架介绍
文件对话框
二维数组求和 练习
Unit test Chapter6
UUID and indexing rules
Introduction to dynamic memory functions (malloc free calloc realloc)
Review of various historical versions of Photoshop and system requirements
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
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
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
"Photoshop2021 tutorial" align and distribute to make dot patterns
测试基础5
树莓派rtmp推流本地摄像头图像
DBUtils
ssm框架整合
Deep Qt5 signal slot new syntax
How idea creates a groovy project (explain in detail with pictures and texts)
写代码涉及到的斜杠/和反斜杠\
Event filter
Counting Nodes in a Binary Search Tree