Pass an address into a function with reference - C++ Data Type

C++ examples for Data Type:Reference

Description

Pass an address into a function with reference

Demo Code

#include <iostream>
using namespace std;
void f(int& r) {
   cout << "r = " << r << endl;
   cout << "&r = " << &r << endl;
   r = 5;/*from   w w  w . j  a v  a 2  s  .c om*/
   cout << "r = " << r << endl;
}
int main() {
   int x = 47;
   cout << "x = " << x << endl;
   cout << "&x = " << &x << endl;
   f(x); // Looks like pass-by-value,
   // is actually pass by reference
   cout << "x = " << x << endl;
}

Result


Related Tutorials