为什么 `&array` 和 `array` 指向同一个地址? [英] Why are `&array` and `array` pointing to the same address?

查看:37
本文介绍了为什么 `&array` 和 `array` 指向同一个地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到现在,我认为数组和指针是一样的.但是我发现了一个奇怪的案例:

代码

int array[5] = { 10,11,12,13,14};std::cout <<数组<

输出

0x7ffeed730ad00x7ffeed730ad00x7ffeed730ad00x7f906d4003400x7ffeed730a300x7f906d400340

如您所见,array&array 具有相同的值.但是 pArray&pArray 具有不同的值.如果数组与指针相同,则数组的地址应与数组不同.array&array 如何相同?如果 array&array 相同,那么保存数组值的内存地址是多少?

解决方案

Plain array 衰减到指向其第一个元素的指针,它等于 &array[0].第一个元素也恰好从与数组本身相同的地址开始.因此 &array == &array[0].

但重要的是要注意类型是不同的:

  • &array[0] 的类型是(在你的例子中)int*.
  • &array 的类型是 int(*)[5].

&array[0]&array 之间的关系可能会更容易一些,如果我更图形化"地展示它(添加指针):

<前>+----------+----------+----------+----------+----------+|数组[0] |数组[1] |数组[2] |数组[3] |数组[4] |+----------+----------+----------+----------+----------+^|&array[0]|&大批

作为额外的补充,array 衰减到指向其第一个元素的指针,即 array 衰减到 &array[0]因此将具有相同的类型.

<小时>

不过,指针的情况有所不同.指针pArray 指向某个内存,pArray 的值是该内存的位置.这就是您使用 pArray 时得到的结果.它也与 &pArray[0] 相同.

当您使用&pArray 时,您会得到一个指向指针的指针.也就是说,您获得了变量 pArray 本身的位置(地址).它的类型是int**.

有点图形化的指针 pArray 应该是这样的

<前>+--------+ +-----------+-----------+-----------+-----------+-----------+-----+|阵列 |----> |pArray[0] |pArray[1] |pArray[2] |pArray[3] |pArray[4] |... |+--------+ +-----------+-----------+-----------+-----------+-----------+-----+^ ^||&pArray &pArray[0]

[注意数组"末尾的...,这是因为指针不保留有关它指向的内存的信息.指针仅指向特定位置,即数组"的第一个"元素.将内存视为数组"取决于程序员.]

Until now, I thought an array is the same as a pointer. But I found a weird case:

code

int array[5] = { 10,11,12,13,14};

std::cout << array << std::endl;
std::cout << &array << std::endl;
std::cout << &array[0] << std::endl;

int *pArray = new int[5];

std::cout << pArray << std::endl;
std::cout << &pArray << std::endl;
std::cout << &pArray[0] << std::endl;

output

0x7ffeed730ad0
0x7ffeed730ad0
0x7ffeed730ad0

0x7f906d400340
0x7ffeed730a30
0x7f906d400340

As you can see array and &array have the same value. But pArray and &pArray have different value. If array is same as pointer, address of array should be different from array. How can array and &array be the same? If array and &array are same, what is the address of the memory which holds the array values?

解决方案

Plain array decays to a pointer to its first element, it's equal to &array[0]. The first element also happens to start at the same address as the array itself. Hence &array == &array[0].

But it's important to note that the types are different:

  • The type of &array[0] is (in your example) int*.
  • The type of &array is int(*)[5].

The relationship between &array[0] and &array might be easier if I show it a little more "graphically" (with pointers added):

+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array

As an extra addendum, array decays to a pointer to its first element, that is array decays to &array[0] and will thus have the same type.


Things are different with pointers though. The pointer pArray is pointing to some memory, the value of pArray is the location of that memory. This is what you get when you use pArray. It is also the same as &pArray[0].

When you use &pArray you get a pointer to the pointer. That is, you get the location (address) of the variable pArray itself. Its type is int**.

Somewhat graphical with the pointer pArray it would be something like this

+--------+       +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+       +-----------+-----------+-----------+-----------+-----------+-----+
^                ^
|                |
&pArray          &pArray[0]

[Note the ... at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]

这篇关于为什么 `&amp;array` 和 `array` 指向同一个地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆