Home > Software engineering >  Why reference to array's object returns the object's address, not the value itself?
Why reference to array's object returns the object's address, not the value itself?

Time:10-24

I've been studying arrays for a while and I struggle to grasp the idea behind these lines of code:

int array[] {1, 2, 3, 4};
std::cout << "The address of the first element is " << &array[0];

Why reference in this case prints the address and not the value? As far as I know reference access the value of the object being referenced, not its address.

CodePudding user response:

int a = 24;
int& r = a; // <- when is part of a type (e.g `int&` here) `&` denotes a reference type

int* p = &a; // <- as an operator `&` is the address of operator
  • Related