0
int arr[]={1,2,3};
int* A=arr;

I wonder if arr is simply an int pointer that points to the first element of the array or if there's any difference between A and arr. Also I wonder: while &arr is the pointer points to the whole array, what does &A point to? I'll appreciate it if somebody could explain how & works for a pointer.

user17732522
  • 53,019
  • 2
  • 56
  • 105
Beluga
  • 9
  • Two questions here. `A` and `arr` are not the same. `&A` points to an `int*`. – Drew Dormann Jan 04 '22 at 18:56
  • Actually, the `A` variable **points to a single integer**, no more no less. – Thomas Matthews Jan 04 '22 at 18:56
  • 2
    Arrays can be confusing, but don't overthink it. `&A` is the address of `A`, and `A` is a pointer-to-int. That's all there is to it. Yes, it points at the first element of an array, but that doesn't affect its type. – Pete Becker Jan 04 '22 at 18:56
  • The `arr[]` is an array, e.g. a contiguous container of integers. Since the capacity was not specified, the capacity will be determined by the compiler from the initialization list. The symbol `arr` represents the location of the first element in the array, where as `&arr[1]` represents the location of the 2nd element. – Thomas Matthews Jan 04 '22 at 18:59
  • 1
    Apart from the difference in `sizeof(A)` vs `sizeof(arr)` you would find very little practical difference between the two. – Sergey Kalinichenko Jan 04 '22 at 18:59
  • 1
    A is a pointer to the first element in the array, not the whole array. And as a consequence A will have lost all information on the size of the array. – Pepijn Kramer Jan 04 '22 at 18:59
  • I think I am still a bit confused about the arr part, Can I summarise it like this: arr is a special pointer whose value is the same with its own address, then why arr points to the first element while &arr points to the whole array – Beluga Jan 04 '22 at 19:04
  • @Beluga -- no, that's not it. `arr` is the name of an array of 3 `int` values. Where it gets confusing is that theme of an array decays into a pointer to its first element in most cases. So in `int *A = arr;` the name `arr` decays into a pointer to its first element, and `A` gets the value of that pointer. If you do, for example, `sizeof(arr)`, the name doesn't decay, and the result is the size of the array. Similarly with `&arr` -- the name doesn't decay, so the type of `&arr` is pointer to array of 3 `int`. – Pete Becker Jan 04 '22 at 19:17

1 Answers1

0

I wonder if arr is simply an int pointer

It isn't. arr is an array, and not a pointer. A is a pointer.

what does &A point to?

Just like &arr points to arr (which is an array), so does &A point to A (which is a pointer).

eerorika
  • 232,697
  • 12
  • 197
  • 326