当前位置:网站首页>Pointer array and array pointer
Pointer array and array pointer
2022-07-04 04:08:00 【Research monk Binbin】
Pointer arrays and array pointers
1. Pointer array
int arr[5] = {
0, 1, 2, 3, 4};
int *ptrArr[5] = {
&arr[0], &arr[1], &arr[2], &arr[3], &arr[4] };
// Another way to express int* ptrArr[5] = {&arr[0], &arr[1], &arr[2], &arr[3], &arr[4] };
cout<<*(ptrArr[3])<<endl; // Output elements 3
The above figure shows the pointer array , It's essentially an array , There is 5 Elements , Each element is a pointer . This * The number is leaning against int side , Or leaning next to the array name does not affect its nature .
You can see arr The memory address of the array is 0x00cff7e8, There is 5 Elements , The size of an element is 4 byte .
Look at the pointer array , You can see in it 5 A pointer to the , This is because when initializing, we point each pointer to an element .0 The pointer of element No. is 0x00cff7e8, just 0 The location of , And so on .
cout<<*(ptrArr[3])<<endl; // Output elements 3
First analyze ptrArr[3], Represents the... In the array 4 Elements , That's the pointer 0x00cff7f4,* The value corresponding to dereference is the element 3.
2. Array pointer
int arr[5] = {
0,1,2,3,4};
int (*arrPtr)[5] = &arr;
An array pointer is essentially a pointer , Pointing to the array , The biggest difference with pointer array is that the asterisk and array name are enclosed in parentheses .
Notice that the type above is int*[5], The type here is int[5]*;
cout<<(*arrPtr)[3]<<endl; // Output elements 3
arrPtr In essence, it is a pointer to an array , Get the array by dereferencing ,(*arrPtr)[3] Is equal to arr[3].
3. A pointer to a two-dimensional array
The memory distribution of the two-dimensional array is as follows .
Hypothetical array a pass the civil examinations 0 The address of the element is 1000, Then the first address of each one-dimensional array is shown in the figure below :
Next, let's look at the application of array pointers in two-dimensional arrays . This is a choice question of Niuke network , The answer is 10, 20, 30.
From the statement int (*p)[3]
With brackets , Enclose the asterisk and the array name , It means this is an array pointer , Point to one 3 Array of elements .
You can see in the memory distribution ,n It represents a 2*3 Array of , and p The declaration of points to a 3 Array of elements , therefore p In essence, it points to the first row of the two-dimensional array . It can also be seen from the above value ,p=n[0], It's all 0x00affea0{10,20,30}.
first p[0][0] Is equal to n[0][0], That's the element 10;
the second *(p[0] + 1), Note here p[0] It's actually n[0],n[0] What is stored is the first address of the first line , That is to say n[0][0] This address , adopt +1 Address offset , In essence, the offset is 4 Bytes (int), It becomes n[0][1] This address , Value by dereference 20.
Third (*p)[2],p What's left is n[0], therefore (p)[2] Equivalent to n[0][2], So the result is 30.
Expand the .p Pointing to 3 Array of elements , namely int[3], that p+1 Just move forward 34=12 byte , That is, the length of a row in a two-dimensional array , namely p+1 Will cause the pointer to point to the next line . Draw the following equivalent conclusion (from C Chinese language network ):
a+i == p+i
a[i] == p[i] == *(a+i) == *(p+i)
a[i][j] == p[i][j] == *(a[i]+j) == *(p[i]+j) == ((a+i)+j) == ((p+i)+j)
give an example ,(p+1) It means the 1 Row data , Because using the whole row of data has no practical significance , When the compiler encounters this situation, it will be converted to point to the line No 0 Pointers to elements ; You can see below , First line n[1] My first address is 0000bafbd8. So for a row of data, dereference is to get the address of the first element .
((p+1)+1) It means the first one 1 Xing di 1 The value of the elements . This is a data , So dereference can get that data . Another way is to ,(p[1]+1).
边栏推荐
- 还原窗口位置的微妙之处
- 2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
- JDBC advanced
- “软硬皆施”,助力建成新型云计算数据中心
- Reduce function under functools
- 深度优先搜索简要讲解(附带基础题)
- Simple dialogue system -- text classification using transformer
- 【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
- Go 语言入门很简单:Go 实现凯撒密码
- JSON string conversion in unity
猜你喜欢
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
Nbear introduction and use diagram
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
1289_FreeRTOS中vTaskSuspend()接口实现分析
Unity 绘制弹球和台球的运动轨迹
选择排序与冒泡排序模板
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
[untitled]
随机推荐
Future源碼一觀-JUC系列
Object oriented -- encapsulation, inheritance, polymorphism
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
Katalon使用script实现查询List大小
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
Tcpclientdemo for TCP protocol interaction
Evolution of MySQL database architecture
【罗技】m720
Storage of MySQL database
JS实现文字滚动 跑马灯效果
Katalon框架测试web(二十一)获取元素属性断言
深入浅出对话系统——使用Transformer进行文本分类
Wechat official account web page authorization
Audio and video technology development weekly | 232
[book club issue 13] multimedia processing tool ffmpeg tool set
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
Mindmanager2022 efficient and easy to use office mind map MindManager
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
vim正确加区间注释