没有一个二维数组衰变的指针的指针 [英] Doesn't a 2D array decay to pointer to pointer
问题描述
到现在为止我是pretty多少确定
Up till now I was pretty much sure that
int arr[4][5];
然后改编
将衰减的指针的指针。
Then arr
will decay to pointer to pointer.
我不知道我怎么得到关于改编
是指针的指针,但它似乎pretty明显,我。因为改编[I]
将是一个指针,因此改编
应该是一个指针的指针。
I am not sure how did I get about arr
being pointer to pointer but it seemed pretty obvious to me. Because arr[i]
would be a pointer, and hence arr
should be a pointer to pointer.
难道我错过了一些东西。
Am I missing out on something.
推荐答案
是的,你是在很多错过了:)
Yep you are missing out on a lot :)
要避免文本的另一面墙上,我会链接到<一个href=\"http://stackoverflow.com/questions/22983734/multiarray-and-multiarray0-and-multiarray0-same/22984784#22984784\">an回答我今天早些时候写道解释多维数组。
To avoid another wall of text I'll link to an answer I wrote earlier today explaining multi-dimensional arrays.
考虑到这一点,改编
是4个元素,每一个都是5 INT $的数组一维数组C $ C>秒。
当一个前$ P $以外&放pssion使用,改编
或 sizeof的改编
,这个衰减到&放大器;常用3 [0]
。但是,什么是&放大器;常用3 [0]
?它是一个指针,并且重要的是,一个右值
With that in mind, arr
is a 1-D array with 4 elements, each of which is an array of 5 int
s.
When used in an expression other than &arr
or sizeof arr
, this decays to &arr[0]
. But what is &arr[0]
? It is a pointer, and importantly, an rvalue.
由于&放大器; ARR [0]
是一个指针,它不能进一步衰减。 (数组衰变,指针不会)。此外,它是一个右值。即使它可能衰变成一个指针,哪来的指针呢?你不能在一个右值点。 (什么是&放大器;?(X + Y)
)
Since &arr[0]
is a pointer, it can't decay further. (Arrays decay, pointers don't). Furthermore, it's an rvalue. Even if it could decay into a pointer, where would that pointer point? You can't point at an rvalue. (What is &(x+y)
? )
看它的另一种方式是记住 INT ARR [4] [5];
20的连续集团 INT
S,分为4个标段的5编译器的脑海中,但在运行时内存中没有特殊标记。
Another way of looking at it is to remember that int arr[4][5];
is a contiguous bloc of 20 int
s, grouped into 4 lots of 5 within the compiler's mind, but with no special marking in memory at runtime.
如果有双重衰退,那么会是什么 INT **
点?它必须指向一个为int *
定义。但在内存中是为int *
?当然也有不是一堆指针徘徊在内存刚刚在这种情况发生时的情况下。
If there were "double decay" then what would the int **
point to? It must point to an int *
by definition. But where in memory is that int *
? There are certainly not a bunch of pointers hanging around in memory just in case this situation occurs.
这篇关于没有一个二维数组衰变的指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!