Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pointers
2.1.
C++ code for pointers
3.
Reference
3.1.
C++ code for reference
4.
Difference between pointers and reference
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference between Pointers and References

gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

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++

Pointers

  • 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;
}

 

Output:

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
Bootcamp

Reference

  • 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

#include<iostream>
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;
}

 

Output:

var = 20
ref = 30

 

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

Difference between pointers and reference

S.No.

Pointer

Reference

1.

It can be initialized to any value after declaration also.

It must be initialized when declared.

2.

It can point to Null.

It can’t point to Null.

3.

Need to be dereferenced with (*)

Can simply use by name

4.

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.

Conclusion

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
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass