Reference vs Pointer in C++

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

In simple words,

Reference- It is an alternative name for an existing variable.
Pointer- It is a variable that contains a memory address/location of another variable.

But essentially, they both render access to another pre-existing variable.

Example

int a = 10; //pre-existing variables
int *b = &a; //b is a pointer variable
int &c = a; //c is the reference 
cout<<*b<<endl<<c;

OUTPUT

10
10

Both b and c rendered the value in a.

Basics first!

Understanding the following operators is crucial in order to understand the code inside out.

  • Reference operator '&'
  • Dereference operator '*'

Reference operator

This is also known as the 'address of' operator, which implies the address of associated variable.

Eg. '&a' means the address of the variable 'a', irrespective of the data type of a.

INPUT

int a=10;
cout<<&a;

OUTPUT

0x7fff794a6ad4

Take note of the 'address of' operator behind a, output value may vary, but it will always be a hex code value referring to the address of a and not the value stored in a.

Dereference operator

As the name suggests, it dereferences, or it retrieves the value stored in the variable pointed by specific pointer.

Example

int a=10;
int *iptr=&a;
cout<<*iptr<<' '<<iptr<<' '<<a<<' '<<&a;

OUTPUT

10 0x7fff794a6ad4 10 0x7fff794a6ad4

The magic of them working together

GUESS THE OUTPUT

int a=10;
int *ptr = &a;
cout<<*&*&*&*ptr;
10
0x7fff794a6ad4
0x7ffe4d18da68
Throws error
The reference and dereference operator shall cancel each other's effect!

So lets get to them individually first.

Reference

As the name suggests, it gives an alternative way to refer a variable.
Syntax for declaration:

type &reference_name = variable name

Example:

int &b = a;
  • b is reference to a.
  • b is known to be alias name of a.

Any changes done to 'b' will be reflected in 'a' and viceversa as they both are essentially referring to the same memory location.

Pointers

A pointer is a variable that contains a memory location of a pre defined variable.
Syntax:

type *pointer_name
pointer_name = address_of_variable

Example:

int a;
int *iptr = &a;

Click here for a deeper understanding of pointers in C++

But both give an alternative reference to variables, so where is the difference?

Initialisation

What is the point of a reference, without anything to refer to? None, right! References must be initialised at declaration
On the other hand pointers can be initialised anywhere after declaration, Pointers may also contain NULL values.

Reassignment

Once you name variable, you do not pass that name to another variable. Same goes to the reference, it isn't a new variable, but an alias name to a pre-existing variable. So the alias name or reference cannot be reassigned to another variable.

int i,j;
int &a=i;
int&a=j; //This will raise error

On the other hand, pointers are variables themselves, and hence can be reassigned.

int i,j;
int *a=&i; //pointer variable a contains address of integer i
a=&j; // pointer variable a now contains address of integer j and not i

Memory Address

The reference and the variable itself, refer to the same memory location and hence have the same memory address.
Whereas pointers are new variables and thus have their own memory location.

Indirection

A pointer could point to any variable, it might as well be a pointer itself, that is pointer to a pointer

int a=10;
int *iptr = &a;
int **pptr = &iptr;

But does a name for a name makes sense to you? Say b is the alias name of a, whenever we state b in the code, we're referring to the corresponging memory location, no new memory location is coming into play. Hence, reference to a reference makes less sense, and isn't exhibited in C++.

int a=8;
int &b=a;
int &&c=b; //This line will throw an error

Arthmetic

Pointer arthmetic exists but Reference arthmetic does not!
Pointer itself is a variable, containing a value and hence it can be incremented and decremented according to its type, and ofcourse that would change the memory location it is pointing to!

A reference on the other hand, may increment or decrement the value in the memory location it refers to, but doesn't increment or decrement the name itself, and ofcourse cannot reassign the variable its pointing to.

Overall, pointers are dynamic and useful, while references are simple and helpful!

So when to use what?

  1. Passing in functions

Functions may receive references as passed by pointer and passed by reference.

Initialisation

return-type function_name( type* , type* ...)  //pass by pointer
return-type function_name( type& , type& ...)  //pass by reference

while a new set of variables have been introduced to work on another value, which need to be dereferenced for changes, simply alias names are created when passed by reference.

Hence, a rule of thumb shall be
Use pass by reference when you can, and pass by pointer when you have to!

Note: Use pass by pointer if you need to maintain backward compatibility with C.

  1. Data Structures

Pointers help us work on a variety of data structures such ass linked list and trees and also show dynamic memory allocation, and hence finds application in speres where references simply cannot get the job done.

  1. NULL and reassignment

Wherever you find application of 'NULL' or might as well require to perform reassignment, go for pointers!

There you have it, references and pointers might look similar in essence but find a larger variety of applications and two very different concepts, and now you must be clear with them and related concepts!

Note - Values of addresses here are for illustration purposes only, and may vary in your system.

With this article at OpenGenus, you must have a complete idea of Reference vs Pointer in C++. Enjoy.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.