2026-06-20·7 min

Pointers vs References in C++ — Like Your Friend's Netflix Account

You know that friend who gives you their Netflix password? That's a reference. You know the friend who just texts you the address of their house party? That's a pointer. Let me explain.

C++ProgrammingBasics
Pointers vs References in C++ — Like Your Friend's Netflix Account

The Netflix Password vs The Party Address

Let me make this dead simple.

A reference is like a person's nickname. When your friend calls you "Champ" all day, they're not making a copy of you — they're just using another name. Whatever you do, whoever you're with, Champ sees the same thing. You and Champ are the same person, just different names.

A pointer is like a phone number written on a sticky note. The yellow square of paper is not your friend — it's a string of digits that tells you how to reach your friend. You can change the sticky note, throw it away, or write a different number on it. The paper is separate from the person.

Both are ways to contact someone. One is the person (nickname), one points to the person (phone number). Same axis, parallel story.

What the Code Actually Looks Like

Think of snacks as your friend, address as the sticky note, and alias as the nickname:

int snacks = 42;    // Your friend "snacks" has 42 items
 
int* address = &snacks;  // Pointer: sticky note with snacks' phone number
int& alias  = snacks;    // Reference: another name for snacks

&snacks means "the address of snacks." *address means "whatever is at that address."

*address = 100;   // Changes snacks to 100 — we went to the address and replaced the snacks
alias = 100;      // Same thing — but we didn't need the address, just used the name

Both lines do the exact same thing. But look at the readability difference:

  • *address = 100 — looks weird, right? Because we're dereferencing.
  • alias = 100 — reads just like normal code. That's the point of references.

The Three Rules That Make References Different

References are "safer" because they have three rules:

1. Must be initialized immediately

int& r;          // ❌ Compile error. You can't just declare a reference.
int& r = x;      // ✅ Must bind to something from day one.

You can't have a "null reference." Unlike a pointer which can be nullptr (pointing at nothing), a reference always points at a real thing.

2. Cannot be reseated

Once a reference is bound, it's locked in. Forever.

int x = 10, y = 20;
int& r = x;      // r is now another name for x
r = y;           // This DOES NOT make r point to y. This assigns y's value to x.
                // x is now 20. r is still x.

With a pointer, you can point at x, then point at y. With a reference, you bind once and that's it.

3. Cannot be null

There is no such thing as a "null reference." A reference, by definition, aliases a real object.

int* p = nullptr;  // ✅ Totally fine — pointer can be null
int& r = *p;       // ✅ Compiles... but crashes when you use r (dereferencing null)
                   // Don't do this.

When to Use Which — The Simple Rule

Default to a reference. When you just want to work with an existing variable without copying it.

// Reference: I just want to use this object
void print(const std::string& s);       // Just looking, not modifying
void append(std::string& s);            // I'm going to modify it
 
// Pointer: "there might not be an object" is part of the deal
Node* next = findNode(id);              // Maybe there's no node with this ID

Reach for a pointer when:

  • "No object" is a valid state (nullptr)
  • You need to reseat — point at different things at different times
  • You're building linked structures (linked list, tree, graph)
struct Node {
    int value;
    Node* next;   // next might be null — that's valid, means end of list
};

The Comparison Table (For When You Need It)

Pointer (T*)Reference (T&)
What it isA variable storing an addressAnother name for a variable
Can be null?Yes (nullptr)No
Must initialize?NoYes
Can reseat?Yes p = &yNo
Syntax*p to read, &x to get addressJust use the name
Dynamic allocationYesNo

The One-Line Summary

Reference = "another name for the same variable." Safe, simple, never null.

Pointer = "I hold an address." Flexible, but more ways to mess up.

When You're Not Sure

Default to a reference. If you find yourself writing if (ptr != nullptr) all the time — that's a sign you might have wanted a reference instead. If you genuinely need "no object" as a valid state, use a pointer.


Reference: based on pointers_and_references.md from the C++ learning notes.