References vs pointers in C++

Difficulty: Beginner

Question

What is the difference between a reference and a pointer in C++? When would you use each?

Answer

This one is a favourite because both features let you refer to another object indirectly, and interviewers want to see whether you understand where they truly differ instead of just saying 'a reference is like a pointer'.

Here is a good mental picture. A pointer is like a sticky note holding someone's address. You can cross the address out and write a different one, you can leave the note blank, and you can even write the address of the address. A reference is a nickname for a person. Once you say 'Bob is also called Bobby', you cannot later decide Bobby means someone else, and there is no such thing as a nickname for nobody.

Translating that: a reference must be initialised when it is declared, and it can never be reseated to refer to something else. A pointer can be declared uninitialised (dangerous), can be null, and can be reassigned. References have no null state in well-formed code, so you skip null checks. Pointers support arithmetic, which is what makes arrays and iterators work; references do not. You can have a pointer to a pointer, and an array of pointers, but not a pointer to a reference or an array of references. Syntactically, you use a reference like the original variable (no dereferencing), while a pointer requires * or ->.

A subtle trap: assigning to a reference does not reseat it, it assigns to the referred object. Writing 'r = y' copies y's value into whatever r refers to. Interviewers love to test this. Another point: under the hood, compilers usually implement references with pointers, but the language guarantees are different, and the standard does not require a reference to occupy storage at all. Do not claim references 'are just pointers'.

When to use which? Use references for function parameters when the argument is mandatory: pass by const reference to avoid copying big objects, and by non-const reference when the function must modify the caller's object. Use references as return types for operators like operator[] or operator=. Use pointers when the object may be absent (nullptr means 'nothing'), when you need to reseat, when you do pointer arithmetic, or when you are dealing with dynamic ownership, and in modern C++ prefer smart pointers for ownership and raw pointers only for non-owning observation.

Finally, mention the dangling reference: returning a reference to a local variable is undefined behaviour, exactly as with a pointer. Also mention rvalue references (T&&), which are a different beast used for move semantics. Bringing that up shows you know the topic goes deeper.

Code examples

Swap with reference and pointer

#include <iostream>

void swapRef(int& a, int& b) { int t = a; a = b; b = t; }
void swapPtr(int* a, int* b) {
    if (!a || !b) return;          // pointers need a null check
    int t = *a; *a = *b; *b = t;
}

int main() {
    int x = 1, y = 2;
    swapRef(x, y);
    std::cout << x << " " << y << "\n";
    swapPtr(&x, &y);
    std::cout << x << " " << y << "\n";

    int& r = x;     // r is another name for x
    r = y;          // assigns y's value to x; r is NOT reseated
    y = 99;
    std::cout << x << " " << r << "\n";
}

After swapRef, x=2 and y=1. swapPtr swaps back (x=1, y=2). Then r = y copies y (2) into x; changing y later does not affect x or r.

Key points

Common follow-up questions

Interview tips

Concepts covered

references, pointers, pass by reference, null

More C++ & STL questions