Pointers and References in C++

Last Updated : 15 Jan, 2026

In C++, pointers and references are both mechanisms used to deal with memory, memory addresses, and data in a program. Pointers are used to store the memory address of another variable, whereas references are used to create an alias for an already existing variable.

Pointers in C++

Pointers in C++ are a symbolic representation of addresses. They enable programs to simulate call-by-reference and create and manipulate dynamic data structures.

  • Pointers store the address of variables or a memory location. 
  • Can be reassigned to point to another variable
  • Can be nullptr
  • Requires dereferencing to access the value
  • Has its own memory address

Syntax

datatype *var_name;

C++
#include <iostream>
using namespace std;

int main(){
    int x = 10; // variable declared
    int* myptr; // pointer variable

    // storing address of x in pointer myptr
    myptr = &x;

    cout << "Value of x is: ";
    cout << x << endl;

    // print the address stored in myptr pointer variable
    cout << "Address stored in myptr is: ";
    cout << myptr << endl;

    // printing value of x using pointer myptr
    cout << "Value of x using *myptr is: ";
    cout << *myptr << endl;

    return 0;
}

Output
Value of x is: 10
Address stored in myptr is: 0x7ffd2b32c7f4
Value of x using *myptr is: 10

Explanation: The above program declares an integer variable 'x' initialized with value 10 and a pointer variable named 'myptr'. The memory address of x is assigned to myptr. Then it prints the value of x, the address stored in myptr, and the value of x obtained by dereferencing the pointer myptr.

References in C++

When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting '&' in the declaration. There are 3 ways to pass C++ arguments to a function: 

C++
#include <iostream>
using namespace std;

int main(){
    int y = 10;

    // ref is a reference to x.
    int& myref = y;

    // changing value of y to 20
    y = 30;
    cout << "value of y is " << y << endl;
    cout << "value of myref after change in value of y is: "
         << myref << '\n';

    return 0;
}

Output
value of y is 30
value of myref after change in value of y is: 30

Explanation: The above program demonstrates the use of references. First, it declares an integer variable 'y' and then creates reference 'myref' which is an alias to y. Changing the value of y also changes the value of myref which can be seen after printing values of both y and myref.

Pointers vs References

Below are key difference between pointers and references in C++ based on their behavior, usage, and memory handling.

FeaturePointerReference
DefinitionA pointer stores the memory address of another variableA reference is an alias (another name) for an existing variable
DeclarationUses * symbolUses & symbol
InitializationCan be declared without initializationMust be initialized at the time of declaration
Null ValueCan be NULL or nullptrCannot be null
ReassignmentCan point to another variableCannot be reassigned to refer to another variable
Memory AddressHas its own memory addressShares the same memory address as the referenced variable
DereferencingRequires dereferencing using *No dereferencing needed
UsageUseful for dynamic memory, arrays, and data structuresUseful for function parameters and operator overloading
SafetyLess safe (can cause dangling pointers)Safer compared to pointers


Comment