in

CPP – References

A reference is another name of the existing variable, once the variable is initialized with the variable then, either the reference or variable name can be used to access the variable.

What is the difference between a reference and a pointer?

  1. You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
  2. A reference must be initialized when it is created. Pointers can be initialized at any time
  3. Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time.

Creating a Reference

So, now we know the difference between a pointer and a reference its time to understand how to create a reference.

Let’s suppose we have a variable

int num = 7;

and we want to create a reference to num. You can do it by initializing a reference with num:

int& r = num;

Remember & is the symbol used to initialize a reference. 

Understanding References

Still, there is some deep concept missing here, I want you to look and analyze the code below:

#include <iostream>
using namespace std;

int main()
{
  int num = 7;
  int& r = num;
  cout << "Values: " << endl;
  cout << num << endl;
  cout << r << endl;
  cout << "Address: " << endl;
  cout << &num << endl;
  cout << &r << endl;
  return 0;
}

So, what do you think the output is? 

Values:
7
7
Address:
0x7ffd25721124
0x7ffd25721124

Crazy right! Address of a reference and address of a variable is same, so as with the values. Address values will be different in your case but same for both the reference and the variable.

What happened here is : Think of a variable name as a label attached to the variable’s location in memory. You can then think of a reference as a second label attached to that memory location. Therefore, you can access the contents of the variable through either the original variable name or the reference.

Oh….yeah, now you have pretty good understanding of the references; let’s practice some programs so that we can use it in the real scenario of a problem. Here, I’m taking an example of swapping because that is the basic one and works fine for it.

1. Reference as parameters

#include <iostream>
using namespace std;
/*In the arguments of swap function  i and j are the references of x and y
 *the value of i and j will be the value of x and y
 */
void swap(int& i, int & j) // Reference As a Parameter
{
  int temp;
  temp = i;
  i = j;
  j = temp;
}
int main()
{
  int x = 8;
  int y = 2;
  cout << "Before Swapping " << endl;
  cout << x << endl;
  cout << y << endl;
  swap(x, y);
  cout << "After Swapping" << endl;
  cout << x << endl;
  cout << y << endl;
}

Before Swapping
8
2
After Swapping
2
8

2. Reference as a return value

#include <iostream>
using namespace std;

int& add(int i, int j)//Reference as a return value
{
  int res;
  res = i + j;
  int& ref = res;
  return ref;
}

int main()
{
  int a = 10;
  int b = 34;
  add(a,b);
  cout << "Address of ref : " << endl;
  cout << &add(a,b) << endl;
  cout << "Value of ref : " << endl;
  cout << add(a,b) << endl;

}

Address of ref :
0x7fff19f02174
Value of ref :
44

Written by Anandesh

I am a #Linux lover and very much excited about technology and new thing. Love to #read_books. Curiosity is my best teacher. I am still☺️ understanding myself.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CPP – Finishing the Basics

CPP – Structures