存储器映射2D /多维数组 [英] MEMORY MAP for 2d / multidimensional arrays
问题描述
任何人都可以请解释视觉作为如何二维数组存储在内存中: 一个,及安培;一,&安培;一个[0],A [0]都具有相同的地址... 这似乎是一个指向自己的方式...这不能是正确的? 这已被窃听我近一年和搜索在网络上已经使我没有容易的答案,以及.....帮助实在是AP preciated ....感谢名单
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("\na = %d ",a);
printf("\n&a[0] = %d ",&a[0]);
printf("\na[0] = %d ",a[0]);
printf("\n&a[1] = %d ",&a[1]);
printf("\na[1] = %d ",a[1]);
printf("\n&a[0][0] = %d ",&a[0][0]);
int *p;
printf("\n\n sizeof(p) = %d ",sizeof(p) );
printf("\n\n sizeof(a) = %d ",sizeof(a) );
printf("\n\n sizeof(&a) = %d ",sizeof(&a) );
printf("\n\n sizeof(&a[0]) = %d ",sizeof(&a[0]) );
printf("\n\n sizeof(a[0]) = %d ",sizeof(a[0]) );
printf("\n\n sizeof(&a[1]) = %d ",sizeof(&a[1]) );
printf("\n\n sizeof(a[1]) = %d ",sizeof(a[1]) );
printf("\n\n 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
不要参考我存储器映射为一个二维数组Ç ...这没有帮助...
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.
这也解释了为什么&放大器;一个
,&放大器;一个[0]
和&放;一个[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.
此外,还有一个规则,如果一个数组中的前pression它不是标的评估一元&安培;
或的sizeof
运营商,它的计算结果为指向其第一个元素:即使用普通的 A
等同于 &放大器;一个[0]
。由于 A [0]
也是一个数组,使用纯 A [0]
等同于&放大器;一个[0] [0]
。这就解释了为什么 A
和 A [0]
也计算为相同的地址&放大器;一个
,&放大器;一个[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]
.
事实上,一个数组并在该数组的第一个元素的adddress的地址是相同的是不是真的令人吃惊:以结构
同样的事情发生,太。鉴于:
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
和&放大器; XA
是同一个地址(尽管有不同的类型)。
You'll find that &x
and &x.a
are the same address (albeit with different types).
这篇关于存储器映射2D /多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!