C++ · Module 7 · Lesson 31 min read

References vs Pointers

References recap

ini
int x = 10;
int& r = x;     // r is another name for x
r = 20;         // x is 20

Side-by-side comparison

FeatureReference int&Pointer int*
Must be initialized✅ yesno (but should be)
Can be null❌ no✅ nullptr
Can be re-seated (point elsewhere)❌ never✅ yes
Syntax to uselike a normal variable*p, p->
Arithmeticnoyes (p + 1)
Can have a "reference to reference"nopointer to pointer: yes
Stored in containersnot directlyyes

When to use which

  • ✅ References: function parameters you modify or read cheaply (const&), returning access to an element you own (operator[]), range-for loops
  • ✅ Pointers: something may be absent (nullptr), you need to re-point, dynamic data structures, C APIs, polymorphic collections (via smart pointers)

Dangling references ⚠️

Never return a reference (or pointer) to a local variable:

arduino
int& bad() { int x = 5; return x; }   // x is destroyed → dangling

Compilers warn about this. Returning a reference to a member or a static/global is fine.

const& extends temporary lifetimes

c
const std::string& s = make_name();   // the temporary lives as long as s

(But only for a local const& binding — not for members or return values.)

Assignment through a reference

r = y; copies y's value into x — it does NOT make r refer to y.

std::reference_wrapper

To store references in a vector: std::vector<std::reference_wrapper<int>> or use std::ref(x).

Example

C++
#include <functional>
#include <iostream>
#include <string>
#include <vector>

struct Wallet { std::string name; double balance; };

Wallet& richest(std::vector<Wallet>& wallets) {       // returning a reference to an element is fine
    Wallet* best = &wallets[0];
    for (auto& w : wallets)
        if (w.balance > best->balance) best = &w;     // pointer: can be re-pointed
    return *best;
}

const Wallet* find(const std::vector<Wallet>& wallets, const std::string& name) {
    for (const auto& w : wallets)
        if (w.name == name) return &w;
    return nullptr;                                    // pointer can express "not found"
}

int main() {
    int x = 10, y = 99;
    int& r = x;
    r = y;                                  // copies 99 into x; r still refers to x
    r += 1;
    std::cout << "x = " << x << ", y = " << y << '\n';

    int* p = &x;
    p = &y;                                 // the pointer can change target
    *p += 1;
    std::cout << "x = " << x << ", y = " << y << '\n';

    std::vector<Wallet> wallets = { { "Cash", 2500 }, { "eSewa", 8200 }, { "Bank", 45000 } };
    Wallet& top = richest(wallets);
    top.balance -= 5000;                    // modifies the element inside the vector
    std::cout << top.name << " now has " << wallets[2].balance << '\n';

    if (const Wallet* w = find(wallets, "eSewa")) std::cout << "Found eSewa: " << w->balance << '\n';
    if (!find(wallets, "Khalti")) std::cout << "Khalti not found\n";

    int a = 1, b = 2, c = 3;
    std::vector<std::reference_wrapper<int>> refs = { a, b, c };
    for (int& v : refs) v *= 10;            // changes a, b and c
    std::cout << "a b c = " << a << ' ' << b << ' ' << c << '\n';
    return 0;
}

/* Output:
x = 100, y = 99
x = 100, y = 100
Bank now has 40000
Found eSewa: 8200
Khalti not found
a b c = 10 20 30
*/