当前位置:网站首页>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 .
边栏推荐
- C # use SQLite
- Helm -- a powerful package management tool for kubernetes applications
- 做好项目管理的10个关键点和5大措施
- 力扣 1184. 公交站间的距离
- 在机器人行业的专业人士眼里,机器人行业目前的情况如何?
- Enterprise operation we media can't "self Hi": the content should be grounded, not advertising
- 【1184. 公交站间的距离】
- Gradle learning set integration
- PCL点云处理之直线点集投影规则化(五十六)
- Error connecting MySQL database with kettle
猜你喜欢

大咖对话:品牌扎堆数藏赛道,下半场的机遇、挑战在哪里?

Gradle learning - getting started with gradle

线段树,,

一种兼容、更小、易用的WEB字体API
![Luogu p2024 [noi2001] food chain](/img/7f/6ccbc19942f0d4a153025346496834.png)
Luogu p2024 [noi2001] food chain

Ranking of engineering project management software
![[which is better to use, apopost or apifox? Just read this!]](/img/58/4dfe5c56d12e8e88b0a7f3583ff0a4.jpg)
[which is better to use, apopost or apifox? Just read this!]
![[icml2022] climate change and machine learning: opportunities, challenges and considerations, 121 ppt](/img/be/6a3f53070c2ffc9610a77d4e910f91.png)
[icml2022] climate change and machine learning: opportunities, challenges and considerations, 121 ppt

Violent recursion - detailed explanation of Queen n & how to optimize with bit operation

Gee - dataset introduction mcd12q1
随机推荐
[cloud native kubernetes] kubernetes cluster advanced resource object staterulesets
Oracle中实现对指定数据分组且获取重复次数
并查集结构
How to refine the top products of the website in the top 10 of the list for three consecutive months?
Ranking of engineering project management software
[icml2022] climate change and machine learning: opportunities, challenges and considerations, 121 ppt
Backgroundworker enables time-consuming operations without affecting interface response
EL & JSTL: JSTL summary
ICML2022 | 图神经网络的局域数据增强方法
Kubernetes scheduling concept and workflow
由斐波那契数列引述到矩阵快速幂技巧
C# 使用SQLite
Website resources
Moving least squares fitting experiment of PCL point cloud processing (62)
ASP. Net core 6.0 data validation based on model validation
Which is the best interface documentation tool at home and abroad?
Implementation of graph structure, from point to edge and then to graph
Web3 security go + security
Luogu p2024 [noi2001] food chain
H5 online CAD background reading and writing CAD files