d引用二维数组
请看这段代码:-
#include<stdio.h>
int main()
{
int arr[2][2]={1,2,3,4};
printf("%d %u %u",**arr,*arr,arr);
return 0;
}
当我编译并执行这个程序时,我得到了相同的 arr 和 *arr 值,这是二维数组的起始地址。 例如:- 1 3214506 3214506
我的问题是为什么取消引用 arr ( *arr ) 不会打印存储在 arr 中包含的地址处的值?
Please look at this peice of code :-
#include<stdio.h>
int main()
{
int arr[2][2]={1,2,3,4};
printf("%d %u %u",**arr,*arr,arr);
return 0;
}
When i compiled and executed this program i got same value for arr and *arr which is the starting address of the 2 d array.
For example:- 1 3214506 3214506
My question is why does dereferencing arr ( *arr ) does not print the value stored at the address contained in arr ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
*arr 是长度为 2 的整型数组,因此与 arr 共享相同的地址。它们都指向数组的开头,这是相同的位置。
*arr is type integer array of length 2, so it shares the same address as arr. They both point to the beginning of their arrays, which is the same location.
在 C 中,二维数组在内存中并不表示为数组的数组;相反,它是一个常规的一维数组,其中需要第一个给定维度才能在执行时计算数组内的正确偏移量。这就是为什么在多维数组中,您总是需要指定除最后一个维度之外的所有维度(这不是必需的);例如,如果您声明一个数组,则
该数组将在内存中表示为总共 2*3*4 个元素的单个数组。尝试访问位置 (i,j,k) 处的元素实际上会转换为访问普通数组中的元素 3*i+4*j+k。从某种意义上说,需要初始维度才能知道在一维数组中放置“行分隔符”的位置。
in C, a 2d array is not represented in memory as an array of arrays; rather, it is a regular 1d array, in which the first given dimension is needed in order to calculate the right offset within the array at execution time. This is why in a multi-dimensional array you always need to specify all the dimensions except the last one (which is not required); for example, if you declare an array like
the array would be represented in memory as a single array of 2*3*4 elements in total. Trying to access the element at position (i,j,k), will actually be translated into accessing the element 3*i+4*j+k in the plain array. In some sense, the initial dimensions are needed to know where to put "row breaks" in the 1d array.