当前位置:网站首页>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
边栏推荐
- 在IPv6中 链路本地地址的优势
- case 关键字后面的值有什么要求吗?
- Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
- Lora sync word settings
- 关于声子和热输运计算中BORN电荷和non-analytic修正的问题
- Daily question 1: force deduction: 225: realize stack with queue
- 2022-07-05 使用tpcc对stonedb进行子查询测试
- Build op-tee development environment based on qemuv8
- BasicVSR_PlusPlus-master测试视频、图片
- 二分图判定
猜你喜欢
【编译原理】做了一半的LR(0)分析器
Advantages of link local address in IPv6
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Attack and defense world miscall
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
config:invalid signature 解决办法和问题排查详解
剑指offer刷题记录1
Memorabilia of domestic database in June 2022 - ink Sky Wheel
NPDP certification | how do product managers communicate across functions / teams?
随机推荐
Chapter 4: talk about class loader again
将MySQL的表数据纯净方式导出
[linear algebra] determinant of order 1.3 n
Aardio - 封装库时批量处理属性与回调函数的方法
Web APIs DOM 时间对象
Pit encountered by handwritten ABA
poj 1094 Sorting It All Out (拓扑排序)
Senior soft test (Information System Project Manager) high frequency test site: project quality management
POJ 1258 Agri-Net
What are the interface tests? What are the general test points?
【LeetCode】19、 删除链表的倒数第 N 个结点
2022-07-05 使用tpcc对stonedb进行子查询测试
做接口测试都测什么?有哪些通用测试点?
剪映+json解析将视频中的声音转换成文本
case 关键字后面的值有什么要求吗?
[leetcode daily clock in] 1020 Number of enclaves
Lora sync word settings
Crawler obtains real estate data
Management background --5, sub classification
How do I write Flask's excellent debug log message to a file in production?