What is the difference between a reference and a pointer in C++?

PointReferencePointer
Must be initializedYesNo
Can be nullNoYes
Can be changed to refer to another variableNoYes
UseLike a normal variableNeeds * and ->
SizeSame as the object (alias)Size of an address
#include <iostream>

int main() {
    int a = 1, b = 2;
    int& ref = a;
    int* ptr = &a;

    ref = 10;              // changes a
    *ptr += 5;             // changes a
    ptr = &b;              // a pointer can point somewhere else
    *ptr = 20;
    std::cout << a << ' ' << b << '\n';
}
Output

15 20

See References and Pointers.


Learn more about the similar topics:
Tutorials
C++ References and const References
C++ Pointers
C++ Dynamic Memory: new, delete and Smart Pointers
Exercises & Assignments
Swap two numbers using a reference function
Manage a resource with unique_ptr
Interview Questions & Answers
What is the difference between a reference and a pointer in C++?
What is the difference between new and malloc in C++?
What is a memory leak in C++ and how to avoid it?
What is nullptr in C++? What is the difference from NULL?
What is RAII in C++?
What is the difference between unique_ptr, shared_ptr and weak_ptr?
What is const correctness in C++?