16
int my_array[5] = {0};
int *my_pointer = 0;

my_pointer = &my_array;  // compiler error
my_pointer = my_array;   // ok

If my_array is address of array then what does &my_array gives me?

I get the following compiler error:

error: cannot convert 'int (*)[5]' to 'int*' in assignment

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
user74564
  • 163
  • 1
  • 1
  • 4

3 Answers3

27

my_array is the name of an array of 5 integers. The compiler will happily convert it to a pointer to a single integer.

&my_array is a pointer to an array of 5 integers. The compiler will not treat an array of integers as a single integer, thus it refuses to make the conversion.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
-1

This would be a fascinating topic for research on neuroscience, semantics, and software development. Even though we can explain the difference between my_array and &my_array, and even though we can repeat the mantra that "arrays are (or are not) pointers", this distinction is still confusing.

Usually when we take the address of something with the "&" operation, we arrive at a completely different value.

int x;
x=5;
cout <<x << " "<<&x<<endl;

First of all, let's challenge this intuition. A pointer can be accidentally equal to the value it is pointing at:

int* x;
x=(int*)(&x);
cout <<"same: "<<x << " "<<&x<<endl;

So in some contexts semantically different things can evaluate to the same thing. But just because two expressions are equal in the current context does not mean that they are interchangeable.

int w=3;
int* ip=&w;
void* vp=&w;
cout <<"Equal: "<<ip<<" "<<vp<<" "<<(ip==vp)<<endl;
cout <<"But not interchangeable: "<<ip+1<<" "<<vp+1<<" "<<(ip+1==vp+1)<<endl;

The same happens with pointers to arrays. A pointer to an array is often technically "equal" to an array itself, but since they have different semantics, there are situations in which they are not interchangeable. Try this one:

int my_array[5] = {5,6,7,8,9};
cout <<my_array[0]<<endl;    // output 5
cout <<(&my_array)[0]<<endl; // outputs the address of the first element
cout <<sizeof my_array[0]<<endl; // outputs 4
cout <<sizeof (&my_array)[0]<<endl; // outputs 20
Sergey Orshanskiy
  • 6,794
  • 1
  • 46
  • 50
-1

&my_array is the address at which the value of my_array is stored, i.e., it is the address of the address of the array and has type int**.

jwodder
  • 54,758
  • 12
  • 108
  • 124
  • 3
    It might also be useful to know that `my_array` and `&myArray[0]` are the same thing. I've seen the latter used in code, probably just for clarity. – Matt Kline May 10 '11 at 21:10
  • 8
    No, `&my_array` does **not** have type `int**`. – Mark Ransom May 10 '11 at 21:12
  • 3
    @slavik: No they aren't. The former is an array, the latter is a pointer. – GManNickG May 10 '11 at 21:22
  • 2
    The type of `&my_array` is actually `int(*)[5]`, not `int**`. – fredoverflow May 10 '11 at 21:27
  • Is `int(*)[5]` actually an array of 5 pointers to 5 integers? How can a pointer point to 5 integers? – user74564 May 10 '11 at 21:56
  • @user74564 `int(*)[5]` is not an array, it is the type of a pointer to an array of 5 integers. An array of 5 pointers to arrays of 5 integers would be `int (*my_array[5])[5]` – Cubbi May 10 '11 at 22:17
  • @GMan: And often when arrays are passed into a function (with a second argument giving their size), they are often passed as a pointer to the first member of the array. The name of an array serves as a pointer to the first element in the array. – Matt Kline May 10 '11 at 23:10
  • 2
    @slavik: No. An array is an array, a pointer is a pointer. The name of an array is an array. An array can be converted into a pointer to the first element implicitly. – GManNickG May 10 '11 at 23:12