References in C++

References in C++

Here, in this page we will discuss about references in C++. A variable when declared as a reference becomes an alternate name for the existing variable by pointing to the existing memory instead of creating a new memory. They are also called as alias variables or link variables.
References in C++

Syntax

data_type &refernce_variabe=value_variable;

Example:

int a=5;
int &z=a;//now z and a have same address
  • In the above example, variables ‘a’ and ‘z’ are pointing to the same memory address
  • So that ‘z’ becomes the alias name for ‘a’
  • Any change in ‘a’ will also reflect change int ‘z’ and vice-versa

Program to demonstate reference variables

Run
#include <iostream>
using namespace std;
int main()
{
  int a=10;
  int &b=a;//b is alias of a 
  int &c=b;//c is alias of b
  cout<<"a:"<<a<<" b:"<<b<<" c:"<<c<<endl;
  c=20;//a,b also becomes 20
  cout<<"a:"<<a<<" b:"<<b<<" c:"<<c<<endl;
  cout<<"&a:"<<&a<<" &b:"<<&b<<" &c:"<<&c<<endl;//a,b,c has same address
  return 0;
}

Output:

a:10 b:10 c:10
a:20 b:20 c:20
&a:0x6ffe34 &b:0x6ffe34 &c:0x6ffe34
  • Initially ‘b’ is made as an alias of ‘a’ by giving the address of ‘a’ to b now ‘c’ is made an alias of ‘b’, as ‘b’ is pointing a’s address ‘c’ also takes the address of ‘a’.
  • Hence ‘a’, ‘b’, ‘c’ are having the same address and become an alias to each other.
  • Now, ‘b’ is changed to 20, as a result, ‘a’ and ‘b’ also becomes 20 because they are having a common memory space.

Differences between Pointer Variables and Reference Variables

Pointer Variables

Reference Variables

Initialized anywhere and any number of timesInitialized only once and at declaration only
Pointer variables have separate memoryReference variable will point to the same memory of the value variable
Pointers are not that secured because of reinitializationThey are secured as they are constant  pointers
It can be void or nullIt cannot be void or null

Advantage

When the reference variable is lost it can be accessed through another alias, but if the pointer is lost, the whole security comes into question.

Applications

  • The internet architecture is developed by C++ under call- by-  reference concept
  • Many other applications like Chat dialogue box, we can observe gtalk box used in video conferencing, teleconferencing and live telecast etc.

Call by reference in C++

Run
#include <iostream>
using namespace std;
void swap(int& x, int& y)		//aliasing a and b 
{
  int temp = x;
  
  x = y;
  y = temp;
  
  cout << "x:" << x << " y:" << y << endl;
} 
int main() 
{
  int a = 2, b = 3;
  
  swap (a, b);		//func_call
  cout << "a:" << a << " b:" << b << endl;
  
return 0;

}

Output:

x:3 y:2
a:3 b:2
  • Here instead of passing address through pointers ‘x’, ‘y’ are made aliases to ‘a’, ‘b’
  • As ‘x’ and ‘a’, ‘y’ and ‘b’ both are made to have the same address, change in one affects others

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription