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

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

问题描述

直到现在,我还认为数组与指针相同.但是我发现了一个奇怪的情况:

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

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;

输出

0x7ffeed730ad0
0x7ffeed730ad0
0x7ffeed730ad0

0x7f906d400340
0x7ffeed730a30
0x7f906d400340

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

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?

推荐答案

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

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:

  • &array[0]的类型是(在您的示例中)int*.
  • &array的类型是int(*)[5].
  • The type of &array[0] is (in your example) int*.
  • The type of &array is int(*)[5].

如果&array[0]&array之间的关系稍微更图形化"(添加了指针),则可能会更容易:

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

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

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.

尽管如此,但指针却有所不同.指针pArray指向某个内存,pArray的值是该内存的位置.这是使用pArray时得到的.它也与&pArray[0]相同.

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].

使用&pArray时,您将获得一个指向该指针的指针 .即,您获得变量pArray本身的位置(地址).其类型为int**.

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**.

一些带有指针pArray的图形,将是这样的

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天全站免登陆