C++
Pointers vs references in C++: when should you use which?
Almost everyone learning C++ gets stuck on the same question: are pointers and references the same thing? Both let you reach an object from somewhere else, but they behave differently and promise different things. Here's the difference with examples.
At some point while learning C++, everyone asks the same question: is a pointer the same as a reference? Both let you reach an object from somewhere else, and both work without copying. But they make very different promises, and once you understand that difference your code becomes both safer and easier to read.
The short answer
- A pointer is a variable that holds an object's address. It can be null (
nullptr), it can later point to another object, and you can do arithmetic on it. - A reference is a second name for an existing object. It must be bound when it's created, it can't be null, and once bound it can't switch to another object.
int a = 10, b = 20;
int* p = &a; // p holds a's address
p = &b; // now it points to b
*p = 30; // b = 30
int& r = a; // r is another name for a
r = b; // careful: doesn't rebind r to b, assigns b's value to a (a = 30)That last line is where students are most often surprised. There's no such thing as "rebinding" a reference; every assignment to a reference goes to the object it refers to.
The difference in function parameters
You'll see both most often in function parameters:
void increment_ptr(int* x) { if (x) ++*x; } // needs a null check
void increment_ref(int& x) { ++x; } // there's always a valid object
int n = 5;
increment_ptr(&n);
increment_ref(n);A function taking a reference tells the caller: "you must give me a valid object". A function taking a pointer says: "you can give me an object, or nothing". The signature communicates intent.
const references: reading without copying
Copying a large object just to read it inside a function is wasteful. const& avoids the copy and guarantees the function won't modify the object:
void print(const std::vector<int>& v) {
for (int x : v) std::cout << x << ' ';
}In modern C++ this is usually the default choice for large objects. Small, cheap-to-copy types (int, double, small structs) are passed by value.
When to use which
- The object must exist and won't change:
const T&. - The object must exist and the function will modify it:
T&. - The object may be absent (optional):
T*, orstd::optionalto be more explicit. - Ownership is being transferred: not a raw pointer, but smart pointers.
- Walking over an array or low-level memory work: a pointer.
Under the hood
In most cases the compiler implements a reference like a pointer; looking at the assembly, you may not see any difference. The difference is in the guarantees the compiler gives you: with a reference, you can't forget a null check, because there's nothing to check. That's why I think a reference is the shortest, most readable way to say "there's always an object here".
There's no need to fear pointers; they're an important part of C++'s power. But like any tool, they make sense in the right place. On the C side, understanding how pointers and arrays relate is one of the best ways to make this topic click.