0
int arr[10]{};

int* ptr = arr;   // legal
int* (&ref) = arr;   // illegal

While pointer and reference are just same when it comes to assembly,
it is a bit hard for me to understand that int* ptr can points to array while int* (&ref) has to define it as const or explicitly show size such int (&ref)[10] = arr

What is the difference?

jadon
  • 29
  • 4
  • 1
    good read: https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in – Alessandro Teruzzi Mar 09 '21 at 10:37
  • `int* decay = arr; int (*ptr)[10] = &arr; int(&ref)[10] = arr;`. – Jarod42 Mar 09 '21 at 10:40
  • `ptr` is just a thing pointing at the first element of the 10 element array `arr`. `ref` is illegal because you are trying to create a reference to the wrong type of thing (@Jarod42 identifies that for you). Try [c++decl](https://linux.die.net/man/1/c++decl) for decoding / explaining strange declarations. And the article @Alessandro Teruzzi points to covers all the stuff you need to know about refs. – Mr R Mar 10 '21 at 06:43

0 Answers0