使用指针时将2D数组用作多个1D数组 [英] Using 2D arrays as multiple 1D arrays when using pointers
问题描述
我使用下面的程序通过指针访问2D数组.
I used the below program to access 2D arrays using pointers.
#include<stdio.h>
void main()
{
int num[3][2]={ {00,01},{10,11},{20,21} };
int i,j;
printf("-----------------------------------");
/* Treating 2d array as 1d array of each row */
for(i=0;i<3;i++)
{
printf("\nThe Address(&num[%d]) is %u \n \n<= WHICH IS THE SAME AS =>\n \n(num[%d]) %u \t",i,&num[i],i,num[i]);
printf("[*(num+%d)] %u \t [(num+%d)] %u ",i,*(num+i),i,(num+i));
printf("\n The Value is %d \n",*num[i]);
printf("\n -----------------------------------");
}
}
这是输出:
-----------------------------------
The Address(&num[0]) is 2140353424
<= WHICH IS THE SAME AS =>
(num[0]) 2140353424 [*(num+0)] 2140353424 [(num+0)] 2140353424
The Value is 0
-----------------------------------
The Address(&num[1]) is 2140353432
<= WHICH IS THE SAME AS =>
(num[1]) 2140353432 [*(num+1)] 2140353432 [(num+1)] 2140353432
The Value is 10
-----------------------------------
The Address(&num[2]) is 2140353440
<= WHICH IS THE SAME AS =>
(num[2]) 2140353440 [*(num+2)] 2140353440 [(num+2)] 2140353440
The Value is 20
-----------------------------------
我了解程序中会发生什么,并且我知道 *(num + i)
用于访问每一行的地址.
I understood what happens in the program and I know that *(num+i)
is used to access each row's address.
但是,为什么 *(num + i)
和(num + i)
都指向相同的地址?
But, why both *(num+i)
and (num+i)
point to the same address?
要访问该特定行(第i行)的值,我们使用 **(num + i)
,这很有意义,因为 *(num + i)
指向第一个1D数组的行的地址,我们可以使用另一个间接运算符取消引用该指针.
To access the value at that particular row (i'th row) we use **(num+i)
which makes sense because *(num+i)
points to the 1st 1D array's row's address and we can use another indirection operator to dereference that pointer.
但是 *(num + i)
和(num + i)
为何都指向相同的地址?这个编译器依赖吗?还是一些未定义的行为?
But how come both *(num+i)
and (num+i)
point to the same address?
Is this compiler dependent? Or some undefined behavior?
请提供尽可能多的信息.
Please provide as much information as possible.
推荐答案
多维数组连续存储在内存中.这是数组的内存布局:
Multi-dimensional arrays are stored contiguously in memory. Here's a memory layout of your array:
[0][0] [0][1] [1][0] [1][1] [2][0] [2][1]
+------+------+------+------+------+------+
| 00 | 01 | 10 | 11 | 20 | 21 |
+------+------+------+------+------+------+
|_____________|_____________|_____________|
^ ^ ^
num[0] num[1] num[2]
在我的机器上,int的大小为4个字节. num
的类型为 int(*)[2]
-它是2个整数数组的指针.语句 num + 1
是一种指针算法,导致向 num
数组开始的地址添加8个字节(即两个整数的大小).
取消引用后- *(num + 1)
仍然是一个指针,它指向与 num + 1
相同的地址.您可以检查以下简短代码以查看发生了什么:
On my machine int has size of 4 bytes.
num
has type int (*)[2]
- it is a pointer to array of 2 ints. Statement num+1
is a pointer arithmetic and results in adding 8 bytes (that's the size of two ints) to the address where num
array begins.
After dereference - *(num+1)
it is still a pointer and it points to the same address as num+1
. You can check this short code to see what is happening:
#include <stdio.h>
int main(void)
{
int num[3][2]={ {00,01},{10,11},{20,21} };
int (*fp)[2] = num+1;
int* fpp = *(num+1);
printf("%d\n", (void*)fpp == (void*)fp);
return 0;
}
输出为1-那些指针拥有相同的内存地址.
The output is 1 - those pointers hold the same memory address.
这篇关于使用指针时将2D数组用作多个1D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!