当前位置:网站首页>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 .
边栏推荐
- 基于OPENCV和图像减法的PCB缺陷检测
- Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
- 联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
- 旷世轻量化网络ShuffulNetV2学习笔记
- I want to know how to open a stock account? Is it safe to open an account online?
- robots.txt限制搜索引擎收录
- Saving images of different depths in opencv
- robots. Txt restrict search engine inclusion
- 查看 jvm 参数
- Pulsar的Proxy支持和SNI路由
猜你喜欢

Optimal transport Series 1

如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?

Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?

Comment réaliser la liaison entre la serrure intelligente et la lampe, la scène du moteur de rideau intelligent dans le timing intelligent?

基于Pytorch完整的训练一个神经网络并进行验证

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

Focusing on green and low carbon, data center cooling has entered a new era of "intelligent cooling"

Pycharm 打开远程目录 Remote Host
![Od modify DLL and exe pop-up contents [OllyDbg]](/img/ff/7249e6e6644376ae048b23bf63b046.jpg)
Od modify DLL and exe pop-up contents [OllyDbg]

Restcloud ETL实践之数据行列转换
随机推荐
旷世轻量化网络ShuffulNetV2学习笔记
Mouse over effect 9
Big orange crazy blog move notice
鼠标悬停效果八
[multi source BFS] 934 Shortest Bridge
Restcloud ETl数据通过时间戳实现增量数据同步
js防抖和节流
Ipmitool download address and possible problems during compilation and installation
【PR #5 A】双向奔赴(状压DP)
Mouse over effect V
SAP ALV汇总跟导出Excel 汇总数据不一致
522. Longest special sequence II
Small program cloud development -- wechat official account article collection
Cluster method synchronous execution framework suona
Pychar open remote directory remote host
7_OpenResty安装
鼠标悬停效果九
Zero foundation self-study SQL course | window function
鼠标悬停效果五
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?