简体   繁体   English

为什么不(&array)[i]取第i个元素的地址?

[英]Why doesn't (&array)[i] take the address of the ith element?

Consider the following code 请考虑以下代码

int tab2[2];
tab2[0]=5;
tab2[1]=3;
std::cout << tab2[1] << std::endl;
std::cout << (&tab2)[1] << std::endl;

As I have read in other topics, an array can decay to pointer at its first element. 正如我在其他主题中所读到的,数组可以衰减到指针的第一个元素。 Then why doesn't the [] doesn't work the same for tab2 and &tab2 in the above code? 那么为什么[]对于上面代码中的tab2和&tab2不起作用呢? What is different? 有什么不同吗?

It's already "converted" as a pointer. 它已经被“转换”为指针。 You can use the [] notation with arrays or pointers... 您可以对数组或指针使用[]表示法...

(&tab2) means you get the address of your array... In a pointer perspective, it's a pointer to a pointer ( ** ). (&tab2)表示你得到数组的地址......在指针透视图中,它是指向指针( ** )的指针。

So you are trying to convert a variable (which is an array) as a pointer. 所以你试图将一个变量(它是一个数组)转换为指针。 Ok, but then you try to access the [1] element, which of course does not exist, as your pointer points to your array's address... Such a notation would expect a second array. 好的,但是你试着访问[1]元素,这当然不存在,因为你的指针指向你的数组的地址......这样的表示法会期望第二个数组。

This expression: 这个表达式:

(&tab2)[1]

Gets you a pointer to an array of 2 ints. 获取指向2个int的数组的指针。 Then uses array syntax on that pointer-to-an-array to get you the 1st 2 element int array after tab2. 然后在该指针到数组上使用数组语法,以获得tab2之后的第2个2元素int数组。

So you have in memory 所以你有记忆

          tab2

          0         1   // index into tab2
          5         3   // values

You get a pointer to the array 你得到一个指向数组的指针

          0         1
 &tab2 -> 5         3

Then you go 1 array of 2 ints past tab2 然后你走过tab2的2个整数数组

          0         1         2        3
 &tab2 -> 5         3         ?        ?
                             /|\
                              (&tab2)[1]

When you use (&tab2) , you are retrieving the address of your array. 使用(&tab2) ,您正在检索阵列的地址。 Your statement itself answers your question. 你的陈述本身就是你的问题。 Had you used (*(&tab2)) , you would have got what you were expecting as output - 3 . 如果你使用过(*(&tab2)) ,你会得到你期望的输出 - 3

What you've done by adding address-of (&) to tab2 is given yourself the memory address of the tab2 pointer, or a pointer to a pointer (int**). 你通过向tab2添加地址(&)来完成你自己给出了tab2指针的内存地址,或指向指针的指针(int **)。 So logically, yes, indexing it makes sense. 所以逻辑上,是的,索引它是有道理的。 However, &tab2 is not the same as tab2. 但是,&tab2与tab2不同。 &tab2 points to the pointer itsefl, while tab2 points to the array. &tab2指向指针itsefl,而tab2指向数组。 By indexing &tab2 with 1, you've told it to look at the next element in sequence, which doesn't exist (or it does exist, but belongs to another variable). 通过索引&tab2为1,你已经告诉它查看序列中的下一个元素,它不存在(或者它确实存在,但属于另一个变量)。 Had you indexed it with 0, this would have been fine as you would be looking at the "root" of the memory sequence. 如果你用0索引它,这将是很好的,因为你会看到内存序列的“根”。

Indexing just tab2 is fine as well obviously, because it points to an array. 仅对tab2进行索引也很明显,因为它指向一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM