C++ References

C++ References refer to another variable. Reference variables act as reference to already existing variables. This means that a single variable can be called in two ways. One way is calling it by its own name and in second way calling by its reference variable.

Create C++ Reference

& operator is used to create reference variable. There are two ways of creating reference variable. The syntax for creating a reference variable is:

The variable and reference variables should have the same data types. Both the variable var and reference variable refvar hold the same value. It is necessary to initialize a reference variable when its created. C++ does not allow to initialize it later. In this case error arises.

The code below shows how to create and initialize a reference variable. It also prints the values present in the variable and its reference variable. Here refvar is an integer reference initialized to var. The & operator creates the reference.

#include <iostream>
using namespace std;
int main () {
   int var = 44;
   int &refvar = var;
   cout << "The value of variable is: " << var << endl;
   cout << "The value of reference variable is: " << refvar << endl;
   return 0;
}

The output of the above code after compilation is:

References are variables that act like pointers. But note that reference variables are different from pointers in a sense that cannot have NULL value. They must point to a variable having some value and memory allocation. Whereas, pointers can have NULL value. Reference variables have a fix variable to which they point, that cannot be changed. Reference variable cannot point to other variable. Each variable should have its own reference variable.

Here is another example, that shows reference variables of float and string types.

#include <iostream>
using namespace std;
int main () {
   string var1 = "String Type";
   string &refvar1 = var1;
   float var2 = 8.5;
   float &refvar2 = var2;
   cout << "The value of variable is: " << var1 << endl;
   cout << "The value of reference variable is: " << refvar1 << endl;
   cout << "The value of variable is: " << var2 << endl;
   cout << "The value of reference variable is: " << refvar2 << endl;
   return 0;
}

The output of the above code after compilation is:

C++ References as Parameters

Just like pointers, C++ programming language allows to pass references as function’s parameters. For example, in the code below, the function sum() takes two arguments of reference type.

#include <iostream>
using namespace std;
int sum(int& x, int& y);
int main () {
   int a = 15;
   int b = 20;
   int c;
   cout << "Before sum, the value of a is: " << a << endl;
   cout << "Before sum, the value of b is: " << b << endl;
   c = sum(a, b);
   cout << "After sum, the result is: " << c  << endl;
   return 0;
}
int sum(int& x, int& y) {
   int temp;
   temp = x + y; 
   return temp;
}

The output of the above code after compilation is:

C++ References as Return Value

A function can have its return value in the form of a reference. That means function can return a reference that points to variable. For example, in the code below, the function has reference variable as its return value. By using reference as return value, the function can be called on the left side of assignment operator (=). The changeElement() function returns a reference to the element of the array arr[].

#include <iostream>
using namespace std;
int arr[] = {4, 7, 8, 12, 15};
int& changeElement( int i ) {
   return arr[i];  
}
// Driver Program
int main () {
   cout << "The original array is: " << endl;
   for ( int i = 0; i < 5; i++ ) {
      cout << "arr[" << i << "] = ";
      cout << arr[i] << endl;
   }
 
   changeElement(1) = 20; // changing value of second element
   changeElement(4) = 7;  // changing value of fifth element
 
   cout << "The new array is: " << endl;
   for ( int i = 0; i < 5; i++ ) {
      cout << "arr[" << i << "] = ";
      cout << arr[i] << endl;
   }
   return 0;
}

The output of the above code after compilation is:

The second and last elements of the array are changed using the reference variable.