当前位置:网站首页>An understanding of & array names
An understanding of & array names
2022-07-06 00:19:00 【It's Yi'an】
A strange phenomenon
#include<stdio.h>
int main(void) {
int arr[5] = { 1,2,3,4,5 };
int(*p)[5] = &arr;
printf("%p\n", p); // After running, you will find that these two values are equal
printf("%p\n", *p);
return 0;
}A strange secondary pointer
#include<stdio.h>
int main(void) {
int arr[5] = { 1,2,3,4,5 };
int(*p)[5] = &arr;
// The program runs successfully , And the results are :1
printf("%d\n", **p); // We can know p=&arr
printf("%d\n", **&arr); // Both are secondary pointers
return 0;
}A strange explanation
An explanation of a strange problem
Pointer to array , The designer made this design in order to distinguish it from pointers to elements : A simple array name represents a pointer to the first element of the array , It needs to open up a separate space for storage , At the same time, the value in the array name space is immutable . If you want to represent the address of the entire array , Just above the simple array name , Improved the level , Change to secondary pointer , But don't give space alone .
Design a rule , When addressing an array name , Is to take the address of the first level pointer . You need a variable to store it , Need a space , This is a normal secondary pointer , But there is no separate space here , Just put the array name ( First level pointer ) Occupy space as its space , At the same time, improve its level , Change to secondary pointer , Mark this pointer ( Special secondary pointer ), Make a type to mark / Represents it :type(*)[]. If you want to dereference this secondary pointer , Just lower its level , Become a normal primary pointer
// These are just assumptions , In order to solve *p=p The problem reference comes up with a convincing explanation .
summary : The array name is a pointer constant ,& The array name is to take the address of the first level pointer , A secondary pointer , But it was given a special rule .
Some normal applications
As I understand it ,&arr Is a special secondary pointer , The type is type(*)[ ].
#include<stdio.h>
int main(void) {
int arr[5] = { 1,2,3,4,5 };
int(*p)[5]; // Define a pointer variable of this type
p= &arr; // Give it the value of this type
printf("%d\n",(*p)[1]); //*p Make it a normal first level pointer :arr
printf("%d\n", arr[1]); //arr: Pointer to the first element of the array
printf("%d\n", *(*p + 1));
printf("%d\n", *(*(&arr) + 1)); //p=&arr
printf("%d\n", *(arr + 1)); //*p=arr
printf("%d\n", arr[1]);
// The results are :2
// Is that much easier to understand
return 0;
}边栏推荐
- What are the functions of Yunna fixed assets management system?
- Hudi of data Lake (1): introduction to Hudi
- Problems encountered in the database
- 小程序技术优势与产业互联网相结合的分析
- 7.5 decorator
- Extracting profile data from profile measurement
- Cloudcompare & PCL point cloud randomly adds noise
- 18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
- Single source shortest path exercise (I)
猜你喜欢

18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)

如何解决ecology9.0执行导入流程流程产生的问题

时区的区别及go语言的time库

小程序技术优势与产业互联网相结合的分析

多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)

notepad++正則錶達式替換字符串

建立时间和保持时间的模型分析

Miaochai Weekly - 8

After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!

关于结构体所占内存大小知识
随机推荐
JS can really prohibit constant modification this time!
Key structure of ffmpeg -- AVCodecContext
Single merchant v4.4 has the same original intention and strength!
How to solve the problems caused by the import process of ecology9.0
MySQL functions
硬件及接口学习总结
选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
Huawei equipment configuration ospf-bgp linkage
How much do you know about the bank deposit business that software test engineers must know?
建立时间和保持时间的模型分析
Wechat applet -- wxml template syntax (with notes)
Classic CTF topic about FTP protocol
FFT 学习笔记(自认为详细)
[QT] QT uses qjson to generate JSON files and save them
软件测试工程师必会的银行存款业务,你了解多少?
Solve the problem of reading Chinese garbled code in sqlserver connection database
Gd32f4xx UIP protocol stack migration record
Doppler effect (Doppler shift)
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
行列式学习笔记(一)