当前位置:网站首页>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
- Web框架介绍
- OFDM 16 lecture 2-ofdm and the DFT
- Explanation of index failure principle and its common situations
- 知识点总结(一)
- 传智教育|软件测试工程师未来的发展方向有哪些?
- 二维数组求和 练习
- Final Cut Pro Chinese tutorial (2) understanding of material window
- Solution: read the files with different names in the two folders and deal with the files with different mappings
- Dialog introduction
猜你喜欢

精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?

Introduction to dynamic memory functions (malloc free calloc realloc)

一、MySQL基础

文件对话框

Typescript details

How do I reset Photoshop preferences? PS method of resetting preferences

The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay

Introduction to Web Framework

How to import PS style? Photoshop style import tutorial

strlen和sizeof的区别
随机推荐
SVN使用详解
【牛客讨论区】第七章:构建安全高效的企业服务
Svn usage details
一道数学题,让芯片巨头亏了5亿美金
pyside2____1.安装和案列
Slashes / and backslashes involved in writing code\
抽卡程序模拟
【报错】:Cannot read properties of undefined (reading ‘prototype‘)
How idea creates a groovy project (explain in detail with pictures and texts)
Final Cut Pro Chinese tutorial (2) understanding of material window
Network protocol details: IP
项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
会议OA之我的审批
一、MySQL基础
【搜索】—— 多源BFS + 最小步数模型
[search] two way search + A*
Complete Binary Tree
[untitled] I is circularly accumulated under certain conditions. The condition is usually the length of the loop array. When it exceeds the length, the loop will stop. Because the object cannot judge
Invert a Binary Tree
OFDM 十六讲 2- OFDM and the DFT