I wrote c++ program to understand main use of references in the program
Location of x = 0x61ff18Code:#include<iostream> using namespace std; int main() { int x = 10; cout << "Location of x = " << &x << endl ; int& ref = x; cout << "Location of ref = " << &ref << endl ; ref = 20; //set ref to 20 cout << "x = " << x << endl ; cout << "Location of x = " << &x << endl ; cout << "Location of ref = " << &ref << endl ; x = 30; // set x to 30 cout << "ref = " << ref << endl ; cout << "Location of x = " << &x << endl ; cout << "Location of ref = " << &ref << endl ; return 0; }
Location of ref = 0x61ff18
x = 20
Location of x = 0x61ff18
Location of ref = 0x61ff18
ref = 30
Location of x = 0x61ff18
Location of ref = 0x61ff18
I can't figure out result x and ref variable have same address how it's possible.
I have read pointer and reference are separate both store memory location what are the main difference between pointer and reference