Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c++ - Why are `&array` and `array` pointing to the same address?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...