当前位置:网站首页>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
边栏推荐
- Export MySQL table data in pure mode
- case 关键字后面的值有什么要求吗?
- 自定义 swap 函数
- 【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
- 【LeetCode】19、 删除链表的倒数第 N 个结点
- 2022-07-05 use TPCC to conduct sub query test on stonedb
- 网络基础入门理解
- 二分图判定
- Web APIs DOM time object
- Anaconda installs third-party packages
猜你喜欢
将MySQL的表数据纯净方式导出
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
软考高级(信息系统项目管理师)高频考点:项目质量管理
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
Spatial domain and frequency domain image compression of images
新手程序员该不该背代码?
0 basic learning C language - interrupt
Attack and defense world ditf Misc
随机推荐
pytorch_ Yolox pruning [with code]
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
基於 QEMUv8 搭建 OP-TEE 開發環境
Learn the principle of database kernel from Oracle log parsing
ThreadLocal详解
poj 1094 Sorting It All Out (拓扑排序)
AdaViT——自适应选择计算结构的动态网络
case 关键字后面的值有什么要求吗?
The ceiling of MySQL tutorial. Collect it and take your time
MySQL ---- first acquaintance with MySQL
0 basic learning C language - digital tube
That's why you can't understand recursion
i. Mx6ull build boa server details and some of the problems encountered
MySQL教程的天花板,收藏好,慢慢看
NetXpert XG2帮您解决“布线安装与维护”难题
Aardio - 通过变量名将变量值整合到一串文本中
Windows Auzre 微软的云计算产品的后台操作界面
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
【LeetCode】19、 删除链表的倒数第 N 个结点
树的先序中序后序遍历