-1

I have created a reference variable but I can't get the expected result. The reference variable is printing some hexadecimal numbers like >>>0x6ffe20

 #include <iostream>
 using namespace std;
 int main(){
      string food = "pizza";
      string &meal = food;
      cout << food << endl;
      cout << &meal << endl;
      cout << "The &meal and food variable are same " << endl;
    
} 
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • `cout << &meal << endl;` on right hand side `&` means `address of`. You already have a reference, and it doesn't need to be taken the address of – The Dreams Wind Aug 24 '22 at 08:25
  • This is explained in any beginner level [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Aug 24 '22 at 08:28

1 Answers1

0

Just do a cout << meal;

With cout << &meal; you are printing the memory address of the reference. The & means address.

Heiko Vogel
  • 172
  • 6
  • In an expression `&` means address-of, in a declaration it means reference. I guess that's where the confusion comes from. – john Aug 24 '22 at 08:31
  • 2
    Actually it's not the memory address of the reference, but the address of the referenced variable. – j6t Aug 24 '22 at 08:32
  • Well, as the reference is just a another name for the same variable, the address of the reference and the address of the referenced variable are the same! – Heiko Vogel Aug 25 '22 at 08:14