devnotes.

References vs Pointers

A reference is an alias for an existing variable — a second name for the same memory — and is one of C++'s biggest usability improvements over raw pointers for many everyday tasks.

int age = 25;
int &ref = age;    // ref is now another name for age — must be initialized here

ref = 30;           // this changes age itself
std::cout << age;   // 30

void increment(int &x) {  // pass by reference — no pointer syntax needed
    x++;
}

int n = 5;
increment(n);        // n becomes 6, no & or * needed at the call site

Unlike a pointer, a reference must be initialized at the moment it's declared, cannot be reseated to refer to a different variable afterward, and cannot be null — these restrictions are exactly what make references simpler and safer to reason about than pointers for many use cases.

References are the idiomatic way to pass arguments by reference in C++ — increment(n) reads and looks just like passing by value, with no &/* clutter at the call site, unlike the C equivalent (increment(&n) with void increment(int *x)). Passing large objects (like a std::string or a std::vector) by const reference (void print(const std::string &s)) avoids an expensive copy while still promising the function won't modify the caller's data — this specific pattern, "pass by const reference," is one of the most common idioms in real C++ code, balancing performance and safety.

Reach for a pointer instead of a reference specifically when you need to represent "no value" (nullptr), when the thing being pointed to genuinely needs to change over its lifetime (reseating), or when working with raw arrays and low-level memory — otherwise, prefer references for their simplicity.

References vs Pointers in C++ | DevNotes