References & Pointers in C++

From the C++ cheat sheet · References & Pointers · verified Jul 2026

References & Pointers

The two indirection mechanisms and when to use each.

cpp
// Quick Reference
int n = 5;
int& ref = n;      // alias - must bind on init, never rebinds
int* ptr = &n;     // address of n
*ptr = 10;         // dereference to read/write
int* none = nullptr;
💡 A reference must bind on creation and can never be reseated; a pointer can be null and reassigned.
⚡ Use nullptr (typed) instead of NULL or 0 for null pointers.
📌 Dereferencing a null or dangling pointer is undefined behavior - guard with if (ptr).
🟢 Reach for references by default; use raw pointers only for optional/rebindable indirection.
pointersreferences
Back to the full C++ cheat sheet