当前位置:网站首页>Master the usage of const
Master the usage of const
2022-06-28 22:54:00 【Herring 29】
stay C About language const Decorated variable :
- C In language const Modified variables are called constant variables , It's not called a constant
- C A constant variable in a language is called a constant variable because it cannot be used as an lvalue , The rest are the same as variables , That is, the value of a constant variable can be modified
- Constant variables can be uninitialized , But if you don't initialize , Then there is no way to assign values directly , Assignment by pointer is required
Let's start with a piece of code :
const int a = 20; // Constant variable
int* p = (int*)&a;
*p = 30;
//a It's a constant variable , Cannot be used as an lvalue , It's essentially a variable , So you can modify the value through the pointer
printf("%d %d %d\n", a, *p, *(&a)); // Output 30 30 30
// Not initialized
const int b;
int* pb = (int*)&b;
*pb = 100;
printf("%d %d\n", b, *pb); // Output 100 100
return 0;What do you think result of the this piece of the code will be ??
Running results :

Let's see C++ in const Decorated variable
- C++ in const The variable decorated is called a constant , Must be initialized , Cannot be used as an lvalue
- C++ in const Compilation method and C Different language ,C In language const Make an appropriate variable to compile the generated instruction ;C++ All appear in const Where constant names are , Are replaced by the initialization of constants test01();
- C++ in const Decorated variables are called constants , But its value can still be modified through the pointer
Let's look at this code :
void test01()
{
const int a = 20; // Constant
int* p = (int*)&a;
*p = 30;
cout << a << " " << *p << " " << *(&a) << endl; // Output 20 30 20
}
What will be output after running ???

Output 20 30 20, When compiling this code , All that appears in the code const The modified variable name will be directly replaced with the initial value , That is to say 20. Using pointer pairs a Change the value of ,a The value of has been changed to 30, Through the dereference of the pointer, you can find that the value of the memory area has been changed to 30;
Let's take another look
Use a variable to initialize const Decorated variable , So this variable is no longer a constant , It's a constant
Code segment
void test02()
{
int a = 20;
const int b = a; // Constant variable
int* p = (int*)&b;
*p = 30;
cout << b << " " << *p << " " << *(&b) << endl;
} The running result must be and C Language is the same .
边栏推荐
- Detailed steps for MySQL to recover data through IBD files
- How many stages did the development and evolution of data analysis go through?
- 国盛证券开户是真的安全可靠吗
- TCP three handshakes and four waves
- Prometeus 2.36.0 新特性
- go语言插件平台的实现思路
- 长投学堂帮忙开证券账户是安全靠谱的吗?个人如何开
- Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
- 00 后云原生工程师:用 Zadig 为思创科技(广州公交)研发开源节流
- 如何使用伦敦金画出支撑阻力线
猜你喜欢

Production environment sonarqube installation

What does project management really manage?

C#/VB. Net to convert PDF to excel

Zadig + 洞态 IAST:让安全溶于持续交付

Windows mysql5.7 enable binlog log

00 后云原生工程师:用 Zadig 为思创科技(广州公交)研发开源节流

Zadig + SonarQube,为开发过程安全保驾

Zadig + sonarqube, ensuring the safety of the development process

Linux Installation mysql5.7 (centos7.6) tutorial

稳!上千微服务如何快速接入 Zadig(Helm Chart 篇)
随机推荐
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
Can we still enter the "pit" data analysis now? Look at the hot jobs of data analysis in 2022!
Qsrand, srand random number generating function in qt5.15 has been discarded
【HackTheBox】dancing(SMB)
Database basic notes
Oracle删除归档日志及添加定时任务
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
如何使用伦敦金画出支撑阻力线
SqlServer复习
flowable 边界定时器
Ansible production environment usage scenario (7): batch deployment of elk clients
Production environment sonarqube installation
Wechat red envelope cover making tutorial and use guide with link jump
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池
Water brother's code
Mono 的执行流程
00 后云原生工程师:用 Zadig 为思创科技(广州公交)研发开源节流
一文读懂,WMS仓储管理系统与ERP有什么区别
时间序列预测系列文章总结(代码使用方法)