当前位置:网站首页>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 .
边栏推荐
- Implementation of go language plug-in platform
- Advanced workplace | understand the "entry" of position advantages
- Mysql通过ibd文件恢复数据的详细步骤
- Mono 的执行流程
- 【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码
- Linux Installation mysql5.7 (centos7.6) tutorial
- [gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
- 【HackTheBox】 meow
- 生产环境sonarqube安装
- How many stages did the development and evolution of data analysis go through?
猜你喜欢

Websocket for im instant messaging development: concept, principle and common sense of mistakes

如何使用伦敦金画出支撑阻力线

The love digital smart 2022 summit opens, sharing data strategy and building data-driven organization methodology

Powerful open source API interface visual management platform Yapi
Oracle set password complexity and timeout exit function

Windows mysql5.7 enable binlog log

After crossing, she said that the multiverse really exists

Deep virtual memory (VM)

PyTorch搭建Transformer实现多变量多步长时间序列预测(负荷预测)

The new version of OpenAPI engine of Kingdee cloud dome is coming!
随机推荐
Simple understanding of counting and sorting
Detailed explanation of Zadig's self-test and joint debugging sub environment for developers
Explanation: Luogu p1762 even number /6.21 internal examination T2
What is the difference between WMS warehouse management system and ERP
Multiomics single cell data integration and regulatory reasoning based on graph linked embedding
Set when quartz scheduled task trigger starts
深入虚拟内存(Virtual Memory,VM)
时间序列预测系列文章总结(代码使用方法)
Oracle deleting archived logs and adding scheduled tasks
Sample code of using redis to realize the like function
Is it safe to open a stock account by mobile phone?
Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池
在长投学堂开通证券账户是安全可靠的吗?
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
[gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
Realization of 2D code generation in micro build low code
The example application of histogram in data visualization makes the public performance results clear at a glance
What is the prospect of VR panoramic production?