C++, End to End
References and Pointers
Pointers and references both let you refer to an object indirectly instead of copying it, and they're used for overlapping reasons, but they're not interchangeable. Getting a feel for when each one is the right tool is one of the more important judgment calls in C++.
Pointer fundamentals
A pointer is a variable that holds a memory address. &x gives you the address of x (the address-of operator), and *p accesses the value a pointer points to (the dereference operator). The same * symbol also declares a pointer type, which is a genuinely confusing overload of syntax worth just memorizing: int* p declares, *p dereferences.
Null pointers and nullptr
A pointer that doesn't point at a valid object should be set to nullptr (C++11 and later; older code used the literal 0 or the NULL macro, both of which are worse because they can be confused with an actual integer zero in overload resolution). Dereferencing a null pointer is undefined behavior, typically a crash, but not guaranteed to be.
Pointer arithmetic, briefly
Pointer arithmetic works the same in C++ as in C (advancing a pointer by 1 moves it forward by sizeof(T) bytes, not one byte), and the same rules and dangers apply: it's only well-defined within a single array (plus one past the end), never across unrelated objects. The C docs course on this site covers pointer arithmetic and its footguns in real depth; in idiomatic modern C++, you'll reach for it directly far less often than in C, since iterators and range-based for cover most of the same ground more safely.
References
A reference is an alias for an existing object, not a separate object that holds an address. Once bound, a reference can't be rebound to refer to something else, and it must be initialized at the point of declaration, there's no such thing as a null reference or an uninitialized one (in well-defined code).
That last line is the single most common reference gotcha: ref = y; looks like it might make ref refer to y instead, but references can't be rebound after initialization. It's always an assignment through the reference to whatever it already refers to.
Pointers vs. references: which to use
A rough, widely-followed default: prefer references for function parameters and return values whenever "referring to an existing object, always valid, never reseated" is what you mean, since references can't be null and don't need null-checking. Reach for pointers when you genuinely need one of the things references can't do: representing "might not point at anything" (a pointer can be null, a reference can't), letting the target change over the pointer's lifetime, or working with C APIs and arrays where pointers are the native currency.
Passing parameters: by value, reference, and pointer
- By value when the function needs its own independent copy, or the type is small and cheap to copy (like
int). - By const reference for read-only access to anything larger than a couple of machine words, avoids a copy with no risk of the callee modifying the caller's object.
- By (non-const) reference when the function needs to modify the caller's actual object.
- By pointer when the target might legitimately be absent (null is a valid state) or the API needs to match an existing C-style interface.
Dangling references and pointers
Both references and pointers can outlive the object they refer to, at which point using them is undefined behavior. The classic case: returning a reference or pointer to a local variable, which is destroyed the moment the function returns.
Most compilers will warn about this specific pattern (returning a reference to a local) with a plain "reference to local variable returned" warning. Always build with warnings on and treat them as errors during development; this is exactly the class of bug they exist to catch.
Try it yourself: spot and fix the dangling pointer›
This function has the same bug as the example above, but with a pointer instead of a reference. Find it, understand why it's wrong, and fix it (one reasonable fix: return by value instead, letting the caller own a real copy).