当前位置:网站首页>Const keyword
Const keyword
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
const keyword
- const Decorated read-only variable
- Save a space , Avoid unnecessary memory allocation , Improve efficiency at the same time
- Modify general variables
- Modify the array
- Modify a pointer
- Modify the parameters of the function
- Modify the return value of the function
- Reference... In another connection file const A read-only variable :
const Decorated read-only variable
A lot of people think that being const The modified value is a constant . This is not accurate , To be precise, it should be a read-only variable , Its value cannot be used at compile time , Because the compiler doesn't know what it stores at compile time .
const The original purpose of the launch , Just to replace precompiled instructions , Eliminate its shortcomings , While inheriting its advantages . Let's look at it and define The difference between macros .( Many people mistakenly think define Is the key word ,define Is the compiler's precompiled instructions , It is implemented by the compiler , No C The content of language .)
Definition const A read-only variable , Have no variability .
const int Max=100;
int Array[Max];
stay Visual C++6.0 Create... Respectively .c Document and .cpp File test . You'll find out .c In file , The compiler will prompt an error , And in the .cpp The file runs smoothly . Why? ? We know that defining an array must specify the number of its elements . This is also confirmed from the side in C In language ,const Embellished Max It's still a variable , It's just a read-only property ;
const Modified read-only variables must be initialized at the same time as they are defined , Why? ?
answer : Because the defined variables are modified into read-only variables , After definition, it cannot be modified , Therefore, the definition must be initialized .
case Whether the statement can be followed by const Modified read-only variables ?
answer : no way ,const It is a read-only variable after modification, but it is still a variable in essence , and case The following statement needs integer 、 Constant of character type 、 Constant expression
Save a space , Avoid unnecessary memory allocation , Improve efficiency at the same time
Compilers are usually not normal const Read only variable allocates storage space , Instead, they are saved in the symbol table , This makes it a compile time value , No storage and read operations , Make it efficient .
#define M 3 // Macro constants
const int N=5; // At this time, the N Put it in memory
......
int i=N; // This is the case N Allocate memory , No more assignments in the future !
int I=M; // Macro replacement during precompiling , Allocate memory
int j=N; // No memory allocation
int J=M; // Replace the macro again , Allocate memory again !
const Defined read-only variables from an assembly point of view , Only the corresponding memory address is given , Rather than image #define The same is given for the immediate number , therefore ,const There is only one copy of the defined read-only variable during the running of the program ( Because it's a global read-only variable , Store in static area ), and #define The macro constant defined has several copies in memory .#define Macros are replaced during the precompile phase , and const The modified read-only variable determines its value at compile time .#define Macro has no type , and const The modified read-only variable has a specific type .
Modify general variables
A general constant is a simple type of read-only variable . This read-only variable is defined , Modifier const Can be used before a type specifier , It can also be used after a type specifier . for example :
int const i=2;
or
const int i=2;
Modify the array
Define or describe a read-only array in the following format :
int const a[5]={
1, 2, 3, 4, 5};
or
const int a[5]={
1, 2, 3, 4, 5};
Modify a pointer
const int *p; // p variable ,p The object pointed to is immutable
int const *p; // p variable ,p The object pointed to is immutable
int *const p; // p immutable ,p Mutable object to point to
const int *const p; // The pointer p and p The objects pointed to are immutable
Ignore type name first ( The compiler also ignores type names when parsing ), Let's see const Which is close to .“ Near water first ”, Decorate who is close to you .
const int *p; //const modification *p,p Is a pointer ,*p Is the object that the pointer points to , immutable
int const *p; //const modification *p,p Is a pointer ,*p Is the object that the pointer points to , immutable
int *const p; //const modification p,p immutable ,p Mutable object to point to
const int *const p; // Previous const modification *p, After a const modification p, The pointer p and p The objects pointed to are immutable
Modify the parameters of the function
const Modifiers can also modify the parameters of a function , Use... When you don't want this parameter value to be accidentally changed in the function body . for example :
void Fun(const int i);
Tell compiler i The in the function body cannot be changed , Thus, some unintentional or wrong modifications of the user are prevented .
Modify the return value of the function
const Modifiers can also modify the return value of a function , Return value cannot be changed . for example :
const int Fun (void);
Reference... In another connection file const A read-only variable :
extern const int i; // Correct statement
extern const int j=10; // error ! The value of a read-only variable cannot be changed .
Note that this is a declaration, not a definition
边栏推荐
- 【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
- NPDP certification | how do product managers communicate across functions / teams?
- LeetCode 练习——剑指 Offer 26. 树的子结构
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- case 关键字后面的值有什么要求吗?
- Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
- 数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
- Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
- RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
- Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
猜你喜欢

Crawler obtains real estate data

Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution

signed、unsigned关键字

Attack and defense world ditf Misc

Senior soft test (Information System Project Manager) high frequency test site: project quality management

关于声子和热输运计算中BORN电荷和non-analytic修正的问题

ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv

Signed and unsigned keywords

rust知识思维导图xmind

剪映+json解析将视频中的声音转换成文本
随机推荐
Sword finger offer question brushing record 1
signed、unsigned关键字
在IPv6中 链路本地地址的优势
extern关键字
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
SQL Server生成自增序号
自定义 swap 函数
That's why you can't understand recursion
Windows Auzre 微软的云计算产品的后台操作界面
Spatial domain and frequency domain image compression of images
go多样化定时任务通用实现与封装
机试刷题1
Web APIs DOM time object
OpenNMS分离数据库
GD32F4XX串口接收中断和闲时中断配置
TypeScript获取函数参数类型
如何用程序确认当前系统的存储模式?
OpenCV VideoCapture. Get() parameter details
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
How do I write Flask's excellent debug log message to a file in production?