Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
C++ code for pointers
C++ code for reference
Difference between pointers and reference
Frequently Asked Questions
Last Updated: Mar 27, 2024

Difference between Pointers and References

Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM


C++ reference and pointer appear to be similar. However, there are a few variations that exist among them. A reference is a variable, and we can call it another name of that existing variable, even as the pointer is a variable that stores the memory address of that existing variable.

Also see, Literals in C, Fibonacci Series in C++


  • A pointer is a variable that consists of the address of another variable.
  •  It may be dereferenced with the assistance of the (*) operator.
  • By using the *operator, we can access that memory location where the pointer points.


C++ code for pointers

// C++ program to demonstrate use of * for pointers in C++
#include <iostream>
using namespace std;
int main()
    // variable declared and value assigned
    int v = 10;
    // A pointer variable p that holds the address of v.
    int *p = &v;
    // This line prints value at address stored in p.
    // Value stored is value of variable "v"
    cout << "Value of v = "<< *p << endl;
    //Address may be different for different machines
    cout << "Address of v = " <<  p << endl;
    // Value at address is now 20
    *p = 20; 
    // This prints 20
    cout << "After doing *p = 20, *p is "<< *p << endl;
    return 0;



Value of v = 10
Address of v = 0x7ffe6ea2b294
After doing *p = 20, *p is 20


Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


  • A reference is a variable like &x =y.
  • this is called some other name for an already present variable. The reference of a variable is created via way of means of storing the address of another variable.
  • A reference variable may be taken into consideration as a regular pointer with automated indirection. Here, automated indirection means that the compiler auto applies the indirection operator (*).


C++ code for reference

using namespace std;
int main()
  // declaring and assigning value to variable
  int var = 10;
  // ref is a reference to x.
  int& ref = var;
  // Value of x is now changed to 20
  ref = 20;
  cout << "var = " << var << endl ;
  // Value of x is now changed to 30
  var = 30;
  cout << "ref = " << ref << endl ;
  return 0;



var = 20
ref = 30


You practice by yourself with the help of online c++ compiler.

Difference between pointers and reference





It can be initialized to any value after declaration also.

It must be initialized when declared.


It can point to Null.

It can’t point to Null.


Need to be dereferenced with (*)

Can simply use by name


It can be changed to point to another variable of the same type.

Once initialized, It cannot be changed to refer to another variable.

Must read, Difference Between Rank and Dense Rank

Frequently Asked Questions

  1. How do pointers relate to references?
    Pointers are generally used to store the memory address of another variable, while references are just another name of the existing variable.
  2. Can we have a reference to a pointer?
    References to pointers are declared in the same way as references to objects.
  3. Can a reference be Null?
    No, references can’t be Null.


This article discussed pointers, references, their syntax, their definitions, concepts, better understanding of the code example, and some differences between their functionalities related to each other.

We hope that this blog has helped you and has enhanced your knowledge regarding Firewalls and if you would like to learn more, stay tuned for more blogs code studio. Do upvote our blog to help other ninjas grow. Happy Coding!"

Thank you for reading.

Previous article
Reference in C++
Next article
Opaque Pointer
Live masterclass