54

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?

9
  • 22
    It's because an array is not a pointer. In the case of an array &a and a are the same thing. In the case of a pointer p is the address the pointer points to, and &p is the address where the pointer value is stored. Commented Feb 21, 2019 at 12:37
  • 7
    Here we can witness the inconsistency of the language. new int[5] should return a pointer to int[5], not a pointer to int. Commented Feb 21, 2019 at 12:42
  • related/duplicate: stackoverflow.com/questions/24104482/… Commented Feb 21, 2019 at 12:45
  • @geza how is that different? A pointer is a pointer Commented Feb 21, 2019 at 14:50
  • 3
    @geza That would require adding the (ugly) variable-length types, or alternative means of creating dynamic arrays. Commented Feb 21, 2019 at 16:23

2 Answers 2

89

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

Sign up to request clarification or add additional context in comments.

18 Comments

Not sure that (void*)&array should be equal to (void*)(&array[0]) BTW. (&array[0] and array should).
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
I mean int (&)[N] decays to int* and so results in same pointer, int (*)[N] and int* are unrelated types. I don't think they have to be equal (once casted to "common" void*) even if it seems simpler to implement it that way.
@Jarod42 I already mentioned the different types, but that doesn't matter. I don't have any quotes from the C++ specification unfortunately, but I distinctly remember that there's something in it that does say that (void*) &array == (void*) &array[0] must still be true (even though it has to be puzzled together from a couple of different sections). Otherwise something like (*(&array))[0] wouldn't work.
@ΝίκοςΑντωνετσής The assignment hello=(int*)&hello is wrong. That you need a cast to silence the compiler should be a sign of that.
|
1

An array of X's has to behave like a pointer to a contiguous list of X's in memory much like a pointer. However, nowhere is it written where the memory that stores that data is must be it's own address and writablej. In the case of an explicit pointer there is a new allocation for this address (in this case the stack) however for an array, on the stack, the compiler already know where the contents are so no new allocation is needed.

As a consequence, its not safe to treat it as a pointer, without indexing. e.g.:

pArray = nullptr; // This is a memory leak, unless a copy is taken, but otherwise fine.
array = nullptr; // This is will make the compiler upset

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.