当前位置:网站首页>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;
}
边栏推荐
- Leetcode 450 deleting nodes in a binary search tree
- About the slmgr command
- 7.5 装饰器
- 建立时间和保持时间的模型分析
- Atcoder beginer contest 254 [VP record]
- Key structure of ffmpeg - avframe
- notepad++正則錶達式替換字符串
- MySQL functions
- Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
- wx.getLocation(Object object)申请方法,最新版
猜你喜欢
行列式学习笔记(一)
Learn PWN from CTF wiki - ret2libc1
云呐|公司固定资产管理系统有哪些?
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
Room cannot create an SQLite connection to verify the queries
云呐|固定资产管理系统主要操作流程有哪些
关于slmgr命令的那些事
云呐|固定资产管理系统功能包括哪些?
Search (DFS and BFS)
随机推荐
Pointer - character pointer
Senparc. Weixin. Sample. MP source code analysis
【DesignMode】组合模式(composite mode)
Upgrade openssl-1.1.1p for openssl-1.0.2k
PHP determines whether an array contains the value of another array
亲测可用fiddler手机抓包配置代理后没有网络
QT QPushButton details
AtCoder Beginner Contest 254【VP记录】
Notepad + + regular expression replace String
Date类中日期转成指定字符串出现的问题及解决方法
LeetCode 1598. Folder operation log collector
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
Hudi of data Lake (2): Hudi compilation
Detailed explanation of APP functions of door-to-door appointment service
Notepad++ regular expression replacement string
MySql——CRUD
硬件及接口学习总结
如何利用Flutter框架开发运行小程序
Leetcode 450 deleting nodes in a binary search tree
XML configuration file (DTD detailed explanation)