Reference vs Pointer In C++

Share Your Love

Although C++ references and pointers appear to be similar, there are some distinctions.  A reference is a variable that holds the name of an existing variable, whereas a pointer is a variable that stores the address of another variable.

What is Reference?

A reference is a variable that serves as a synonym for a previously defined variable. A variable’s reference is made by storing the address of another variable.

A reference variable can be thought of as an automatic indirection constant pointer. In this case, automatic indirection means that the compiler applies the indirection operator (*) automatically.

Example of reference:

int &a = i;

‘a’ is an alias name for the ‘i’ variable in the above declaration. The ‘i’ variable can also be referred to using the ‘a’ variable.

Let’s understand through an example.

#include <iostream>
using namespace std;
int main()
{
   int i=8;    // variable initialization
   int &a=i; // creating a reference variable
   cout<<"The value of 'i' variable is :"<<a;
   return 0;
}  

We’ve generated a reference variable, ‘a’ for the ‘i’ variable, in the above code. We can use the ‘a’ variable to obtain the value of I after constructing a reference variable.

What is Pointer?

A variable that contains the address of another variable is known as a pointer. To access the memory location to which the pointer points, it can be dereferenced using the (*) operator.

Differences between Reference and Pointer:

The following are the differences between reference and pointer:

Definition:

A reference variable is a different name for a variable that already exists. It’s most commonly used in ‘pass by reference,’ when the reference variable is supplied as a parameter to the function, and the function that receives this variable works on the original copy of the variable.

Let’s understand through a simple example.

#include <iostream>
using namespace std;
void func(int &);
int main()
{
   int a=10;
   std::cout <<"Value of 'a' is :" <<a<< std::endl;
  func(a);
  std::cout << "Now value of 'a' is :" <<a<< std::endl;
  return 0;
}
void func(int &m)
{
   m=8;
}  

Output:

Value of 'a' is :10
Now value of 'a' is :8  

A pointer, on the other hand, is a variable that holds the address of another variable. It facilitates programming by storing the memory address of a variable.

Declaration:

By placing a ‘&’ sign before a variable, we can declare it as a reference variable. This symbol will be considered as an address operator if it appears in the expression.

We must first declare a pointer variable before utilising it, and this variable is generated by preceding a variable with a ‘*’ operator.

Reassignment:

The reference variable cannot be reassigned. Now consider the following simple example:

#include <iostream>
using namespace std;
void func(int &);
int main()
{
  int i;    // variable declaration
  int k;    // variable declaration
  int &a=i;
  int &a=k; // error
  return 0;
}  

Multiple declarations of int &a are not allowed, as shown in the above code. As a result, the above code finds that the reassignment operation for the reference variable is invalid.

The pointers, on the other hand, can be reassigned. This reassignment comes especially helpful when working with data structures like linked lists and trees.

Memory Address:

Both the reference and actual variables in the case of reference relate to the same address. The new variable will not be assigned to the reference variable until the original variable is destroyed or no longer exists.

Let’s understand this scenario through an example:

#include <iostream>
using namespace std;
void func(int &);
int main()
{
  int i;
  int &a=i;
  std::cout << "The address of 'a' variable is : " <<&a<< std::endl;
  std::cout << "The address of 'i' variable is : " <<&i<< std::endl;
  return 0;
} 

Output:

The address of 'a' variable is : 0x7fff078e7e44                                                                        The address of 'i' variable is : 0x7fff078e7e44

Both the reference variable and the real variable have the same address, as shown in the output above.

Both the pointer variable and the actual variable will have distinct memory addresses in the case of pointers. Let’s look at an example to see how this works.

#include <iostream>
using namespace std;
int main()
{
    int k;
    int *p;
    p=&k;
    cout<<"The memory address of p variable is :"<<&p;
    cout<<"\nThe memory address of k variable is :"<<&k;
    return 0;
}  

Output:

The memory address of p variable is :0x7ffcc5c164b8                                                           The memory address of k variable is :0x7ffcc5c164b4
Null Value:

The NULL value cannot be set to the reference variable, but it can be assigned to the pointer variable.

Indirection:

Pointers can have a pointer to pointer relationship, allowing for multiple levels of indirection.

#include <iostream>
using namespace std;
int main()
{
 int *p;
 int a=8;
 int **q;
 p=&a;
 q=&p;
std::cout << "The value of q is : " <<*q<< std::endl;
return 0;
} 

The pointer ‘p’ in the above code points to variable ‘a,’ while ‘q’ is a double pointer referring to ‘p.’ As a result, the address of ‘a’ variable would be the value of ‘p’ variable, and the address of ‘q’ variable would be the address of ‘p’ variable.

Output:

The value of q is : 0x7ffd104891dc

It is not possible to refer to another reference in the case of References. If we attempt to write a C++ program, we will receive a compile-time error.

Let’s understand this scenario through an example:

#include <iostream>
using namespace std;
int main()
{
  int a=8; // variable initialization
 int &p=a; // creating a reference variable for ?a? variable.
 int &&q=p;  // reference to reference is not valid, it throws an error.
 return 0;
}  

Output:

main.cpp: In function 'int main()':
main.cpp:18:10: error: cannot bind 'int' lvalue to 'int&&'
int &&q=p;

Arithmatic Operations:

As we all know, arithmetic operations may be done to pointers, which is known as “Pointer Arithmetic,” but they cannot be applied to references. In C++, there is no such thing as Reference Arithmetic.

Let’s see a simple example of Pointers.

#include <iostream>
using namespace std;
int main()
{
 int a[]={1,2,3,4,5}; // array initialization
  int *ptr;  // pointer declaration
  ptr=a; assigning base address to pointer ptr.
  cout<<"The value of *ptr is :"<<*ptr;
  ptr=ptr+1;  // incrementing the value of ptr by 1.
  std::cout << "\nThe value of *ptr is: " <<*ptr<< std::endl;
  return 0;
} 

Output:

The value of *ptr is :1
The value of *ptr is: 2

Let’s understand the references through an example:

#include <iostream>
using namespace std;
int main()
{
 int value=90;  // variable declaration
 int &a=value;   // assigning value to the reference
 &a=&a+5 // arithmetic operation is not possible with reference variable, it throws an error.
 return 0;
}

Because arithmetic operations are not allowed with references, the above code will fail at compilation time.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter