0

I am working with pointers and reference variable in C++.

int i=43;
int &refi=i;

What I know is the variable refi is bound to variable i. However it does not have its own memory. It is just a another name to the variable i. Then how can a pointer point to such unstored variable.

#include<iostream>

int main(){
  int i=43;
  int &refi=i;
  int *p=&refi;
  std::cout<<p;
  return 0;
  }

However I am not getting any error for the above code. Instead I am getting address of it.Am I wrong with the concept of reference variable here?If yes how?

The output is

0x61ff04
The-coder
  • 61
  • 7

1 Answers1

1

The reference has the same address in memory as the variable it's referencing. That essentially makes it an alias. Thus, when you take the address of a reference, you get the address of the original variable.

Aplet123
  • 33,825
  • 1
  • 29
  • 55