问题描述
任何人都可以直观地解释一下二维数组如何存储在内存中:a , &a, &a[0] , a[0] 都具有相同的地址...这似乎是一个以某种方式指向自己的指针……这不可能是对的……这已经困扰我近一年了,在网上搜索也让我找不到合适的答案.....帮助真的很感激....thanx
Can anyone please explain visually as to how the 2D array is stored in memory as:a , &a, &a[0] , a[0] all have the same address...It seems like a pointer pointing to itself in a way...and that cant be right...This has been bugging me for nearly a year and searching on the web has lead me to no apt answer as well..... help is really appreciated....thanx
enter code here
#include<stdio.h>
int main()
{
int a[2][3]={10,20,30,40,50,60};
int row =0,col=0;
printf("&a = %d ",&a);
printf("
a = %d ",a);
printf("
&a[0] = %d ",&a[0]);
printf("
a[0] = %d ",a[0]);
printf("
&a[1] = %d ",&a[1]);
printf("
a[1] = %d ",a[1]);
printf("
&a[0][0] = %d ",&a[0][0]);
int *p;
printf("
sizeof(p) = %d ",sizeof(p) );
printf("
sizeof(a) = %d ",sizeof(a) );
printf("
sizeof(&a) = %d ",sizeof(&a) );
printf("
sizeof(&a[0]) = %d ",sizeof(&a[0]) );
printf("
sizeof(a[0]) = %d ",sizeof(a[0]) );
printf("
sizeof(&a[1]) = %d ",sizeof(&a[1]) );
printf("
sizeof(a[1]) = %d ",sizeof(a[1]) );
printf("
sizeof(&a[0][0]) = %d ",sizeof(&a[0][0]) );
}
输出
&a = 2293536
a = 2293536
&a[0] = 2293536
a[0] = 2293536
&a[1] = 2293548
a[1] = 2293548
&a[0][0] = 2293536
sizeof(p) = 4
sizeof(a) = 24
sizeof(&a) = 4
sizeof(&a[0]) = 4
sizeof(a[0]) = 12
sizeof(&a[1]) = 4
sizeof(a[1]) = 12
sizeof(&a[0][0]) = 4
不要让我参考二维数组的内存映射C ...那没有帮助...
Don't refer me to Memory map for a 2D array in C ... that didnt help ...
推荐答案
所以数组 a
是一个对象占用一块内存:
So the array a
is one object taking up a block of memory:
| a |
这是一个长度为 2 的数组,所以如果我们绘制组成它的元素,它看起来像这样:
It's an array of length 2, so if we draw in the elements that make it up it looks like this:
| a[0] | a[1] |
a[0]
依次是一个长度为 3 的数组,如下所示:
a[0]
in turn is an array of length 3, and looks like this:
| a[0][0] | a[0][1] | a[0][2] |
a[1]
看起来是一样的,所以我们可以重绘数组 a
看起来像这样:
a[1]
looks the same, so we can redraw the array a
to look like this:
| a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] |
注意 a
、a[0]
和 a[0][0]
都位于内存中的同一点:对象 a
的开始.但是,它们确实有不同的大小:a
是整个二维数组",a[0]
是常规数组,而 a[0][0]
是单个 `int.
Notice that a
, a[0]
and a[0][0]
are all located at the same point in memory: the start of the object a
. However, they do have different sizes: a
is the entire "2D array", a[0]
is a regular array, and a[0][0]
is a single `int.
这就解释了为什么 &a
、&a[0]
和 &a[0][0]
是相同的地址(尽管它们有不同的类型)——它们是位于内存中同一点的事物的地址.
This explains why &a
, &a[0]
and &a[0][0]
are the same addresses (though they have different types) - they're the addresses of things located at the same point in memory.
此外,有一条规则是,如果数组在不是一元 &
或 sizeof
运算符的主题的表达式中计算,则计算为指针到它的第一个元素:也就是说,使用普通的 a
等效于 &a[0]
.由于 a[0]
也是一个数组,使用普通的 a[0]
相当于 &a[0][0]
.这解释了为什么 a
和 a[0]
也评估为与 &a
、&a[0]
和 &a[0][0]
相同的地址.
Additionally, there's a rule that if an array is evaluated in an expression where it's not the subject of the unary &
or sizeof
operators, it evaluates to a pointer to its first element: that is, using a plain a
is equivalent to &a[0]
. Since a[0]
is also an array, using a plain a[0]
is equivalent to &a[0][0]
. This explains why a
and a[0]
also evaluate to the same addresses as &a
, &a[0]
and &a[0][0]
.
数组的地址和数组中第一个元素的地址相同这一事实并不令人惊讶:同样的事情也发生在 struct
上.鉴于:
The fact that the address of an array and the adddress of the first element in that array are the same isn't really surprising: the same thing happens with struct
, too. Given:
struct { int a; int b; } x;
您会发现 &x
和 &x.a
是相同的地址(尽管类型不同).
You'll find that &x
and &x.a
are the same address (albeit with different types).
这篇关于二维/多维数组的内存映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!