# References vs pointers in C++

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

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-references-vs-pointers
Last updated: 2026-11-28

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

- A reference must be initialised, cannot be null in valid code, and cannot be reseated.
- A pointer can be null, reassigned, and used with arithmetic.
- Assignment through a reference assigns to the referred object; it never rebinds.
- Prefer const T& for read-only parameters of large types; use pointers when 'no object' is a valid state.
- Returning a reference or pointer to a local variable is undefined behaviour.

## Common follow-up questions

- Can you have a reference to a pointer? A pointer to a reference?
- What is the difference between T&, const T& and T&&?
- Is a reference guaranteed to take up memory?

## Interview tips

- Use the nickname versus address analogy briefly, then move to precise rules.
- Mention the 'assignment does not rebind' trap before the interviewer asks.

## Concepts covered

references, pointers, pass by reference, null

Previous: [What are the key differences between C and C++, and between struct and class?](https://www.internhack.in/learn/interview/cpp-interview/cpp-c-vs-cpp-struct-class) · Next: [const pointer vs pointer to const, and const correctness](https://www.internhack.in/learn/interview/cpp-interview/cpp-const-correctness) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [Stack vs heap memory in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-stack-vs-heap)
- [new/delete vs malloc/free](https://www.internhack.in/learn/interview/cpp-interview/cpp-new-delete-vs-malloc-free)
- [Memory leaks, dangling pointers and double free](https://www.internhack.in/learn/interview/cpp-interview/cpp-memory-leaks-dangling-pointers)
- [RAII: Resource Acquisition Is Initialization](https://www.internhack.in/learn/interview/cpp-interview/cpp-raii)
- [Constructors in C++: default, copy and move](https://www.internhack.in/learn/interview/cpp-interview/cpp-constructors-copy-move)
- [Shallow copy vs deep copy](https://www.internhack.in/learn/interview/cpp-interview/cpp-shallow-vs-deep-copy)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
