当前位置:网站首页>Const and the secret of pointers
Const and the secret of pointers
2022-07-01 02:47:00 【Just call me Xiao Qin】
Preface
The content of the article is read by 《C Experts programming 》 Sort it out . I hope it can help you solve the problem of pointer assignment and const Problems in , I also hope you can correct the mistakes in the article , Common progress .
Assignment of pointer
problem
Set a type to char** The value of is assigned to a const char** Is the object of type legal ?
Let's start with the results , stay vs Under the environment of , The compiler will not report any errors or warnings .
But in linux Environment with gcc The following warning will appear when compiling :
warning: assignment from incompatible pointer type
Warning : Assignments from incompatible pointer types
For the portability of the code, we obviously can't write such code , Now let's explore the mystery step by step .
First, let's understand ANSI C Relevant standards
ANSI C Criteria for simple assignment
To make the assignment form legal , One of the following conditions must be met :
1. Both operands are pointers to compatible types of finite or infinite qualifiers
2. The type pointed to by the left pointer must have all qualifiers of the type pointed to by the right pointer
There is also a description of the type :
const float* Type is not a finite determiner type —— Its type is “ Point to a person with const Qualifier float Pointer to type ”, in other words const Qualifiers are used to decorate the type pointed to by the pointer , Not the pointer itself .
Problem solving
Before solving the problem , Let's first look at a group of simple .
char* and const char*
char* and const char* It is a match. . The reason why it is legal , Because in the following code :
char* cp;
const char* cpp;
cpp = cp;
- The left operand is a pointer to const Qualifier char The pointer to ;
- The right operand is a pointer to a non qualified char The pointer to ;
- char The type and char Types are compatible , The type pointed to by the left operand has the qualifier of the type pointed to by the right operand ( nothing ), Plus its own qualifier (const).
Be careful , On the contrary, the assignment cannot be made .
char* cp;
const char* cpp;
cp = cpp; // At this time, the left operand does not have the right operand const qualifiers
char** and const char**
From the above knowledge, we can know ,char** and const char** Are pointer types without qualifiers , But they point to different types ( The former points to char*, The latter points to const char*), This violates the first rule of the assignment criteria above , So they are incompatible .
It's a little difficult to understand this in this way . It can be understood in the following way :
char** pp1;
const char** pp2;
pp2 = pp1;
- The type of the left operand is const char**, It's a point const char* Pointer to type , and const char* Is a pointer without qualifier , It points to a with const Limited char type ;
- The type of right operand is char**, It's a point char* The pointer to , and char* Is a pointer without qualifier , It points to a that has no qualifier char type .
const char* and char* It's compatible , And they have no qualifiers of their own , So meet the constraints of the standard , The assignment between the two is legal . but const char** and char** The relationship between them is different , Although neither has a qualifier , But the object types they point to are incompatible , Therefore, assignment cannot be performed .
const modification
const Modifying variables
First , keyword const You can't turn a variable into a constant ! Add... Before a symbol const The qualifier simply means that the symbol cannot be assigned . in other words const Decorated variables are read-only , Cannot be modified directly , But it cannot prevent being modified indirectly .
for example :
#include <stdio.h>
int main()
{
const int i = 10;
int* p = &i;
printf("before:%d\n", i);
*p = 20;
printf("after:%d\n", i);// Here the print value becomes 20, The description can be modified indirectly
return 0;
}
const Modify a pointer
const Decorated pointer variables have many positions , Now we will introduce one by one .
const int* p
notes :const int* p And int const p The writing is different , It's the same thing .
This way of writing means :const modification p, Cannot dereference (p) Directly modify the pointed variable , But it can be modified by changing the way the pointer points p.
for example :
#include <stdio.h>
int main()
{
// Modifying the compiler by dereferencing directly below will directly report an error
//int i = 10;
//const int* p = &i;
//*p = 20;
int i = 10;
const int* p = &i;
printf("before:%d\n", *p);
int j = 20;
p = &j;// By changing p The direction of , It can be modified indirectly *p value
printf("after:%d\n", *p);
return 0;
}
int* const p
This way of writing means :const modification p, It cannot be modified by changing the pointer *p Value , But you can dereference (*p) Directly modify the pointed variable .
for example :
#include <stdio.h>
int main()
{
int i = 10;
int* const p = &i;
printf("before:%d\n", *p);
*p = 20;// Can't change p The direction of , However, you can directly dereference the modified value
printf("after:%d\n", *p);
return 0;
}
const int* const p
This way of writing is to modify p and *p, It can't change p The direction of , Nor can it be directly modified by dereference .
边栏推荐
- [graduation season · advanced technology Er] - summary from graduation to work
- kubernetes资源对象介绍及常用命令(二)
- RestCloud ETL实践之无标识位实现增量数据同步
- Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
- ssh配置免密登录时报错:/usr/bin/ssh-copy-id: ERROR: No identities found 解决方法
- RestCloud ETL WebService数据同步到本地
- Mouse over effect 8
- 鼠标悬停效果四
- Lenovo x86 server restart management controller (xclarity controller) or TSM method
- What are the top ten securities companies? In addition, is it safe to open an account online now?
猜你喜欢

The mobile edge browser cannot open the third-party application

Record a service deployment failure troubleshooting

视觉特效,图片转成漫画功能

Introduction to kubernetes resource objects and common commands (II)

Voici le programme de formation des talents de SHARE Creators!

Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and

Here comes the share creators budding talent training program!

5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?

Share Creators萌芽人才培养计划来了!

UE4 rendering pipeline learning notes
随机推荐
Dart training and sphygmomanometer inflation pump power control DPC
【PR #5 A】双向奔赴(状压DP)
[graduation season · advanced technology Er] - summary from graduation to work
Lenovo x86 server restart management controller (xclarity controller) or TSM method
Image preloading in JS
I want to know how to open a stock account? Is it safe to open an account online?
鼠标悬停效果七
How to use Jieba participle in unity
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
鼠标悬停效果五
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
Mouse over effect IV
Prototype and prototype chain in JS
SAP ALV汇总跟导出Excel 汇总数据不一致
js中的图片预加载
Proxy support and SNI routing of pulsar
SSH configuration password free login error: /usr/bin/ssh copy ID: error: no identities found solution
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
使用ipmitool配置X86服务器的BMC网络和用户信息
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip