0

I have a silly doubt . Say suppose I have a array int a[]={3,4,5,6} now I want to know if the address of this array points to the location of only 3 or the entire array elements?

  • An array is an array. However `a` can decay to a pointer to the first element of the array in certain contexts – UnholySheep Aug 08 '22 at 13:32
  • 2
    The address of `a` will point to the first element of `a` (`a[0]`). – mdf Aug 08 '22 at 13:32
  • 6
    The address of an array points to the array. It just so happens that that location is also the address of the first element of the array. – NathanOliver Aug 08 '22 at 13:33
  • 1
    A pointer to the first element of an array, only points to the first element of the array. However by incrementing that pointer you get a pointer to the second element of the array, and so on. So essentially you are correct. – john Aug 08 '22 at 13:34
  • 2
    @mdf -- it's more subtle than that. The address of the array is the address of the array. It doesn't point at anything, because it's not a pointer. The **name** of the array decays into a pointer to its first element in most contexts. So `int *p = a;` sets `p` to point at the first element of the array. – Pete Becker Aug 08 '22 at 14:08

1 Answers1

1

The address of the array points to the array. And that is the same address as the address of the first element.

However, they are not the same type. &a is a pointer to an array with 4 elements, int(*)[4]. &a[0] is a pointer to a single int, an int*. Because arrays can decay to pointers to their first element (for example when passed to a function) those two are often confused.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185