当前位置:网站首页>About constant modifier const
About constant modifier const
2022-07-24 22:29:00 【ima_ zhan】
Has been to const Is a simple constant modifier , Define an immutable variable 【 Notice that here are variables , in other words const Called constant modifier , But in fact, it defines a variable , It is only limited by the compiler and cannot be modified 】.
After listening to the teacher's class, I found const There are many interesting uses :
1:const int i = 10; --> i The value of cannot be changed , if i = 11; False report
2:const int *j = (int*)&i; and int const *j = (int*)&i; --> j The content of the variable pointed to is immutable , if *j = 20; False report
3:int * const t = (int*)&i; --> t The address pointed to cannot be changed , if t++; False report
The teacher's advice is to see const What's ahead , Then that part cannot be modified , such as int const i = 5; const Before is int, said i Cannot be modified
int const * j; const Before, too int, said i The value of the variable pointed to cannot be modified
and int * const j; const Before is int *, Indicates that the value pointed to by the pointer cannot be modified
There is another interesting one that you can try by yourself , I don't know why ,
const in a = 10;
int *p = (int*)&a;
int *j = (int*)&a;
cout << *p << " " << a << " " << *j << endl;
cout << p << " " << &a << " " << j << endl;
*p = 20;
cout << *p << " " << a << " " << *j << endl;
cout << p << " " << &a << " " << j << endl;The result of the above code is very interesting , After execution *p = 20; after ,*p and *j All become 20, however a still 10. At the same time, you can see p and j Address to and a The address of is the same .

I found the answer in others' blogs
http://blog.chinaunix.net/uid-26853826-id-3365403.html
const Global variables are stored in the global storage space , Its value is only readable , Do not modify ;
const Local variables are stored on the stack , Its value can be modified through the pointer ;
const Variables are processed in preprocessing , The compiler reads its value only once .
边栏推荐
- Error connecting MySQL database with kettle
- Time series data in industrial Internet of things
- Get the solution to the slow running speed of Mengxin Xiaobai computer! ٩ ( ‘ ω‘ )و get! ٩ ( ‘ ω‘ )و
- CSF cloth simulation filtering for PCL point cloud processing (59)
- Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays
- PCL点云处理之移动最小二乘拟合实验(六十二)
- DDos攻击分类
- 工程项目管理软件排名
- SVM——针对线性可分(下)
- Push information to wechat through enterprise wechat self built application
猜你喜欢

PCL点云处理之均匀采样抽稀(六十一)

Archsummit: evolution of the underlying framework of cherished microservices

通讯异常判断之通讯心跳信号应用编程

Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises

Helm —— 强大的 Kubernetes 应用的包管理工具

Segment tree,,

The kettle job implementation runs a kettle conversion task every 6S
![洛谷 P2024 [NOI2001] 食物链](/img/7f/6ccbc19942f0d4a153025346496834.png)
洛谷 P2024 [NOI2001] 食物链

Uniform sampling and thinning of PCL point cloud processing (61)

工程项目管理软件排名
随机推荐
CSF cloth simulation filtering for PCL point cloud processing (59)
PCL点云处理之移动最小二乘拟合实验(六十二)
Leetcode 226. flip binary tree
AVL tree of ordered table
Flex layout
Sensor experiment - 485 air temperature and humidity
Random sampling and thinning of PCL point cloud processing randomsample (60)
Ue5 reports an error using the plug-in quixel Bridge
CA证书制作实战
企业运营自媒体不能“自嗨”:内容要接地气不能接广告
Helm -- a powerful package management tool for kubernetes applications
Morris traversal
【ICML2022】气候变化与机器学习:机遇、挑战与考虑,121页ppt
洛谷 P2024 [NOI2001] 食物链
GB、MB、KB分别是什么意思。大小分别是多少
QT learning vs creating QT items shows instances where object references are not set to objects
Esp32485 air temperature and humidity test
EL & JSTL: JSTL summary
How static code analysis works
What do GB, MB and KB mean respectively. What are the sizes