EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Top Differences Tutorial C++ Reference vs Pointer
 

C++ Reference vs Pointer

Priya Pedamkar
Article byPriya Pedamkar

Updated March 18, 2023

C++ Reference vs Pointer

 

 

Difference Between C++ Reference and Pointer

There is a significant difference between C++ reference vs pointer. A reference in C++ is an alternate name for an already existing variable. Once a reference variable is initialized, it can be used to refer to the same variable by another name. On the other hand, a pointer in C++ is a variable that stores the memory address of another variable. Like any variable, they are declared first, and then any variable’s address can be stored in them.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

C++ Reference vs Pointer: Head-to-Head Comparison (Infographics)

Below are the top 7 differences between C++ Reference vs Pointer:

C++-Reference-vs-Pointer-info

C++ Reference vs Pointer: Key Differences

Both are popular choices in the market; let us discuss some of the major differences:

  • The primary difference between C++ Reference vs Pointer is that the former is referring to another variable while the latter is storing the address of a variable.
  • References do not change an original variable, while if the pointer changes, it affects the original variable.
  • A reference must initialize on the declaration, while it is not necessary to initialize a pointer after its declaration.
  • One can create an array of pointers but not an array of references.
  • One cannot assign a null value to a reference but can assign it to a pointer.

C++ Reference vs Pointer Comparison Table

The primary comparisons are as follows:

The basis of comparison  C++ Reference Pointer
Variables A reference is simply an alias for an existing variable. The primary use of a reference variable is to serve as a pass-by-reference parameter in a function. When a reference variable passes to a function, the function operates on the original variable rather than a copy, which would be the case in pass-by-value. Any changes to the original variable inside the function will reflect outside of it as well. Pointer variables store memory addresses rather than values of a specific data type such as int, double or char. These variables simplify programming by providing a way to reference the location of a particular variable in memory.
Declaration Declaration of a reference variable is done by adding an ampersand (&) symbol before an existing variable’s name. When used with an expression, the ampersand symbol denotes the “address of” operator. A reference variable provides an alternative name or alias for an existing variable.

Syntax:

type &newName = existingName;

// or

type& newName = existingName;

// or

type & newName = existingName;

For eg:

/* Test reference declaration and initialization */
#include <iostream>
using namespace std;
int main() {
string name = "Priya";          // Declare a string variable called name
string & refName = name; // Declare a reference (alias) to the variable name
// Both refName and name refer to the same value of “Priya”
cout << name << endl;    // It prints value of the variable “Priya”
cout << refName << endl; // It prints value of reference “Priya”

Now change the value of the Name

refName = "Karishma"; // Re-assign a new value to refName
cout << refName << endl;
cout << name << endl;    // Value of number also changes "Karishma"
name = "Snehal";   // Re-assign a new value to name
cout << namer << endl;
cout << refName << endl; // Value of refName also changes "Snehal"
}

This signifies that a reference variable’s values can change in both the original and copy of the variable.

One must declare the pointer variable before using it in the program. To declare a pointer variable, one prefixes the variable name with an asterisk “*” symbol to indicate that it is a pointer. The pointer variable is accompanied by the data type of the variable it is intended to point to, such as int or double.

Syntax:

type *pntr;   // Declare a pointer variable called pntr as a pointer of type

// or

type* pntr;

// or

type * pntr;

For e.g.:

int * newPtr;     // Declare a pointer variable called newPtr pointing to an int (an int pointer)

This pointer will hold the address. That address holds an int value.

double * newdPtr;  // Declare a double pointer

The “*” indicates that a pointer is being declared and does not act as an operator.

Reassignment One cannot reassign a reference variable.

Example:

int x = 5;
int y = 6;
int &r = x;
A pointer can be reassigned, and this feature comes in handy when a developer is implementing data structures like linked lists, trees, etc.

Example:

int x = 5;
int y = 6;
int *p;
p =  &x;
p = &y;
Memory Address A reference variable shares the same memory address as the original variable. This means that a reference can pass to different functions and be stored in different classes. It always refers to the original variable until that variable goes out of scope or one deletes it. A pointer has its own memory address and stores it on the stack. A pointer is an independent variable and can have new values assigned to itself.
Null Value A reference cannot have a null value assigned. A pointer can have a null value assigned directly.
Arguments A reference variable can be referenced bypass by value. Here arguments are passed by value to the functions. A clone is made and sent to the function using it. Changes made to the copy have no effect on the original variable. When we wish to change the original copy, then it can be done by passing a pointer of the object into the function. This is known as a pass-by reference.
When to use References are indirectly accessing a variable. Consider the following:

Example:

enum day
{
Sunday, Monday, ...
};
//If we define a variable
day x;
//And we want to overload an operator to the statement then we can write as below:
day &operator++(day &d)
{
d = (day)(d + 1);
return d;
}

In short, it has usage in function parameters and reference types.

Using pointers is without any pre-declaration.

day *operator++(day *d);

One can use it to implement data structures and pointer arithmetic operations.

Conclusion

Both C++ Reference and Pointer have their own uses. Although both are difficult to work on, they improve the efficiency of the program to a great extent. One can use pointers to implement data structures and algorithms and utilize references to use functions and parameters with return types.

Recommended Article

Here are some further comparison articles for improving understanding:

  1. Python vs C++ differences
  2. C vs C++ Performance – Best Differences
  3. C++ Vector vs Array: Top Differences
  4. MongoDB vs DynamoDB: Functions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW