0

While initializing the pointer p in the given statement

int a[10], *p;
p = &a[0];

I am in doubt that whether the type of &a[0] is pointer to int or is it int?
I came to this confusion after reading this:

Using a as a pointer to the first element in the array, we can modify a[0]: *a = 7;.

NOTE: I am interested to know the type of &a[0].

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 4
    `a[0]` is an int, so `&a[0]` is a pointer to an int. `&(something)` is a pointer to that thing. What you're confused about is assignment to a pointer, since assignment works differently when `*` prefixes something – Kristopher Micinski Aug 02 '13 at 18:01
  • Down voter leave your comment.. – haccks Aug 02 '13 at 18:24

4 Answers4

2

&a[0] is a pointer to int. Here are the interpretation steps for that:

  • &a[0] is parsed as &(a[0]) because the subscript operator [] binds more tightly than the address operator &.
  • a is an array, but it is automatically converted to a pointer to its first element in this case. (It would not be converted if it were the operand of sizeof, _Alignof, or & or were a string literal used to initialize an array.)
  • a[0] takes the pointer and becomes an lvalue for the first element (element 0).
  • &(a[0]) is a pointer to a[0]. Since we know a[0] is an int, a pointer to a[0] is a pointer to an int.

Then p is assigned the value of &a[0]. Thus, p is a pointer to the first element of a.

Since p is a pointer to the first element of a, *p is an lvalue for that element. An lvalue can be used on the left side of an assignment to assign to the object it refers to.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • In the statement `p = a`, isn't `a` is of `integer array type` and promoted to `pointer to int`? – haccks Aug 02 '13 at 18:29
  • @haccks This is array decaying into a pointer: http://stackoverflow.com/a/1461466/1708801 – Shafik Yaghmour Aug 02 '13 at 18:36
  • @ShafikYaghmour; I only want to know about type of `a` when it is assigning to a pointer `p`. – haccks Aug 02 '13 at 18:40
  • 1
    @haccks It will be an `int *`. – Shafik Yaghmour Aug 02 '13 at 18:46
  • 1
    @haccks: In `p = a`, `a` is converted to a pointer to `int`. I simply call it a conversion, not a promotion or a decay. Any array object in an expression (not just a simple identifier; it could be computed in various ways, such as dereferencing a pointer to an array) is automatically converted to a pointer to the first element of the array unless the array is the operand of `sizeof`, of `_Alignof`, or `&` or is a string literal used to initialize an array. – Eric Postpischil Aug 02 '13 at 18:55
  • 1
    Yes, in the statement `p = a`, the type of `a` itself isn't really relevant. The type of the *expression* `a`, in this particular context, is pointer to `int`, because of the automatic conversion. Even though the type of `a` is an array type, the type of the *expression* `a` is not necessarily the same, and in this case, is not. – Crowman Aug 02 '13 at 19:00
1

& is the address operator so, &a[0] is taking the address of the first element of the array a, so it is an int *. Since [] has higher precedence than * you can read it as follows:

&(a[0])

which may be more clear. The case where you have:

p = a ;

works do to the array decaying to a pointer.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

for historical reasons

 p = a; 

and

p = &a[0] 

are equivalent. Beware that

auto q = &a; 

is also legal but the type of q will NOT be int* but rather int[10] *. This is probably not what you wanted.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
0

The address of a[0] is of type pointer to an integer. You can modify the first element of the array because *a is equivalent to *(a + 0) which is nothing but a[0].

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26