当前位置:网站首页>Combination of const and pointer
Combination of const and pointer
2022-07-28 11:03:00 【tutu-hu】
One .const Analysis of several cases of combination with pointer :
Definition int a=1;int b=2;
1.int const *p = const int *p; //p It can change itself , But it points to int Type data value is immutable
understand :p Is a pointer variable , The pointer is the address , therefore p What is stored is the address , The definition form here represents p It's variable in itself , That is to say p Value ( Address ) Is variable , But it can't be modified p( Address ) Value stored in , That is to say p variable , however *p immutable .
therefore :
int const *p=&a; p=&b; Correct use , because p In itself ( Address ) variable .
int const *p=&a; *p=3; Wrong use , because p The value of the point ( The value in the address ) immutable .
2.int * const p; //p It's immutable , But it points to int Type data value is variable
understand :p The value of itself is an address , therefore const p Express p( Address ) immutable , but p Point to data ( The value stored in the address ) Is variable, that is p immutable , but *p Is variable .
therefore :
int * const p=&a; *p=3; Correct use , because *p( The contents of the address ) variable .
int * const p=&a; p=&b; Wrong use , because p In itself ( Address ) immutable .
3.const int * const p; //p It's immutable ,p Point to the int Type data values are also immutable
understand : Combined with the above two situations, it is not difficult to analyze , here p Oneself ( Address ) immutable , And point to the data value ( The value in the address ) Also immutable .
therefore :
const int * const p=&a; *p=3; Wrong use , because *p( The contents of the address ) immutable .
const int * const p=&a; p=&b; Wrong use , because p( In the address ) It's immutable .
summary :const Decorating pointers has a wide range of uses , For example, we are making string arrays copy You will use :void stcopy(char *des,const char *src); Use here const I don't want to decorate src The value of the array is changed in the function .
about const There are the following ways to use form memory in combination with pointers : It mainly depends on const It's direct modification p still *p, Simply put, look const The modifier is in * Front or back , Feel for yourself .
边栏推荐
猜你喜欢

剑指 Offer 06. 从尾到头打印链表

Inventory: 144 free learning websites, the most complete collection of resources in the whole network

BC35 NB模块AT指令开发总结

GKSpheresNoiseSource

Here is a super practical excel shortcut set (common + summary of eight categories)

CRM+零代码:轻松实现企业信息化

I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime

用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案

剑指 Offer 30. 包含min函数的栈

GKCylindersNoiseSource
随机推荐
Install MySQL based on docker
分体式测斜探头安装要点及注意事项
Header library file
栈和队列
Network file system service (NFS)
GKARC4RandomSource
Inventory: exciting data visualization chart
Fhwy workday schedule
Using k-means clustering to classify tariff models of different industries
Eslint, eslint Chinese document
21. Merge two ordered linked lists
keil和IAR中lib库文件的生成和使用
GKRidgedNoiseSource
Causes and solutions of invalid ROM table
Zero code | easily realize data warehouse modeling and build Bi Kanban
GKPerlinNoiseSource
GKCoherentNoiseSource
5. Implement MapReduce program on window side to complete wordcount function
Arduino Basics
Tree shaking and DCE