What is the difference between a pointer and a reference in C++?
Both give indirect access, but with different rules.
- A reference must be initialised when declared and can never be reseated to refer to something else. A pointer can be null and can be reassigned.
- A reference cannot be null — there is no valid way to create one from nothing — so a function taking a reference need not check.
- Syntax. A reference is used like the object itself; a pointer must be dereferenced with
*or->. - Pointer arithmetic exists; reference arithmetic does not.
- You can have a pointer to a pointer, but not a reference to a reference.
How to choose: use a reference when the parameter is mandatory and use const& to pass large objects without copying. Use a pointer when the argument is genuinely optional, when you need to reseat it, or when interfacing with a C API. std::optional is often clearer than a nullable pointer for an optional value.
Note: The important caveat is that a reference is not inherently safe — it can dangle. Returning a reference to a local variable, or holding a reference to a vector element after the vector reallocates, are both undefined behaviour despite the reference never being null.





