Introduction

A reference variable is one that refers to the address of another variable. It represents the name of another variable, location, or value. Once you initialize the variable references, that variable will be referred to using the variable name or reference name.

The variable reference is nothing but an alternate name for the already existing variable.

Creating the C++ Reference

The developers created the reference by using the symbol ampersand (&), to easily identify the variable as reference with the help of this symbol.

Syntax:

Data_type Variable_name =&existing_variable;

Data_type: It is the basic data type that has been used for declaration.

Variable_name: It is a reference variable name used to identify the existing variable. It is like an identifier.

&existing _variable: It represents the reference or address of the existing variable.

Sample code:

String s=”anova”;

String s1=&s;

In the above example the ‘s’ is a normal variable, s1 as the reference variable which holds the address of s string.

Source Code

#include <iostream>

int main() {

   int a=10;

   int &b=a;

   std::cout<<a<<"\n";

  std::cout<<b<<"\n";

 std::cout<<&b<<"\n";

 std::cout<<b<<"\n";

return 0;

}

Output:

C_Plus_Plus_References_2

In the above example code, ‘a’ was declared as an integer data type and it assigned it with the value of 10. Then the reference variable b is declared as integer type and it is assigned with the addressed variable.

When you use it to print the output with the variable, a will return as 10. Likewise, b also returns as 10. When you print the variable &b will return the address of a variable.

Mainly this address will access the location in memory. Most of the Operating system programs are written by C++ references only.

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

C++ Reference Applications

You will use the C++ reference in two different ways in C++ programming. They are as follows:

  1. Reference as a parameter in C++
  2. Reference as a return value in C++

C++ Reference as Parameter in a Function:

A function can change the value of a variable if it receives a reference to it. For instance, references are used to exchange the following program variables.

A function may declare a reference variable as arguments. The variable references pass the value. Suppose the user has made any changes, it will change the original value of the referenced variable.

Source Code:

Consider the program to swap the two numbers using the reference variable as parameter:

#include <iostream>

void swap_out(int& d1, int& d2);// function declaration

int main()

 {

 int d1 = 100;

   int d2 = 200;

   std::cout << "Before swap,value 1 is" << d1 <<"\n";

  std::cout << "Before swap, value 2 is :" << d2 <<"\n";

swap_out(d1, d2);// function calling

  std::cout << "After swap data, value1 :"<< d1 <<"\n";

  std::cout << "After swap, data value2 :"<< d2 <<"\n";

return 0;

}

void swap_out(int& d1, int& d2)

 {

   int temp;

   temp = d1; /* save the value at address d1 */

   d1=d2;;    /* put d2 into d1 */

   d2 = temp; /* put d1 into d2 */

  return;

}

Output:

C_Plus_Plus_References_3.

Use the C++ Reference as Return Value in Function:

The function also returns the reference variable to the assignment expression. When a function returns a reference, it implicitly returns a pointer to the value it returns. You can utilize a function on the left side of an assignment statement in this manner.

This is nothing but a function as a reference. While you use the return statement, it will be considered as a reference variable only.

Consider the addition of two numbers using the reference return.

Source Code

#include <iostream>

double vals = 24.1;

double& setValues( int i );

int main() {

   std::cout << "Value before change" <<"\n";

  std::cout << vals<<"\n";

   setValues(20.23) ;

   std::cout << "Value after change" <<"\n";

   std::cout << vals<<"\n";

   return 0;

}

double& setValues( int i ) {

vals=i;

   return vals;   // return a reference

}

Output:

C_Plus_Plus_References_4.

How is the C++ reference different from the pointer?

  • Both the C++ reference and pointer are used to change the value of a local variable from one function to another function.
  • They are both used to save the copy of the object passing value in a function.
  • You can declare The pointer as void but the reference not as void.

Example:

pointer:  void *a;

But reference cannot be a void.

  • References can have only one level of indirection, but pointers have multiple levels of indirections.

Example:

int x=100; //simple or ordinary variable.

int *b=&x; //single pointer

int **ptt=&b; //double pointer

int ***ptrr=&ptt; //triple pointer

but the references are only one level of indirection. That is:

int ab=5; //simple or ordinary variable

         int &Sa=ab;

         int &Ss=Sa;

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

The C++ Reference Is Less Powerful Than the Pointer

  • A reference can't be reseated or made to refer to another object after it's been formed. You frequently used pointers for this.
  • In reference, NULL references are not allowed. To signal that a pointer isn't leading to anything useful, it's common to make it NULL.
  • A reference must be initialized at the moment of declaration, although pointers do not have this requirement.
  • There is no need to use the two different symbols like & and *.
  • In reference, you can use only one special symbol called ampersand (&).
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

A C++ reference is a useful tool, which helps to access the address of the other variable. It used to change the variable of the addressed variable. In the above content, you may conclude that the C++ reference is the easiest tool and especially more powerful when there is no pointer concept in any other programming language. It easily accesses the original data with corresponding location. If you make any changes at any particular point, it will automatically change the original location of the variable.

In this article you learned about stack, implementation, operations with syntax and examples. To get expertise in C++ programming you can join Simplilearn’s C++ certification training course. To benefit budding learners, Simplilearn has made a special selection of courses available for free. Join Simplilearn for free Simplilearns free courses and revolutionise your profession with certificates, specialisations, and dozens of other courses.

If you are on the other hand looking for a more comprehensive full stack software development learning, you should check out Simplilearn’s Post Graduate Program in Full Stack Web Development in collaboration with Caltech CTME. This bootcamp style program has everything - tools, skills, languages, practical exposure, certifications and endorsements - that you’d need to become a sought-after full stack developer in any of today’s big companies; anywhere in the world.

Have any questions for us? Leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!