Interview Q&A · C++ · 10 questions in this topic

Pointers vs References — C++ Interview Q&A

All C++ topics

The heart of C++ interviews — pointers, references, const correctness, dangling, and ownership thinking. Part of Pointers, References & RAII · 213 questions in the C++ bank.

What is the difference between a pointer and a reference?

Answer

A pointer holds an address: it can be null, reseated, and needs explicit dereference (*p). A reference is an alias: it must bind at initialization, cannot be null or reseated, and is used with plain value syntax.

Guideline: references for required aliases (parameters, range-for), pointers for optional/reseatable indirection and C interop.

Key points

  1. 1Null — pointers yes, references no (a null reference is UB).
  2. 2Reseat — pointers yes (p = &other), references no.
  3. 3Syntax — pointers use -> and *, references use . directly.

Example

Example.cppcpp
int x = 1, y = 2;
int* p = &x; p = &y; // reseated, fine
int& r = x; // r aliases x forever
// int& bad; // error: must initialize

💡 Must-bind-once plus cannot-reseat is the complete reference contract in one line.

What is const correctness and what do the const positions on pointers mean?

Answer

Const correctness uses the type system to forbid mutation mistakes: const member functions, const references, const pointers. Reading right-to-left decodes pointer consts: `const int*` is pointer-to-const (cannot change the pointee), `int* const` is const-pointer (cannot reseat), `const int* const` is neither.

East-const style (`int const*`) reads uniformly right-to-left and avoids the single exception in west-const parsing.

Example

Example.cppcpp
const int* a;      // pointee is const (reseat ok)
int* const b = nullptr; // pointer is const (reseat banned)
// const int* const c;  // both const
void read(const std::string& s); // const-correct in-param

💡 Read pointer declarations right-to-left out loud — interviewers accept that as the decoding method.

What is a dangling pointer/reference and how do dangling bugs happen?

Answer

A dangling pointer/reference points at storage whose lifetime ended — a destroyed stack local, a deleted heap object, a reallocated vector element, or a temporary that died at the semicolon. Using it is undefined behavior that may appear to work until optimization or reuse exposes it.

Common factories: returning locals by reference/pointer, storing string_view/c_str views past owner death, holding iterators across vector reallocation, and use-after-free.

Example

Example.cppcpp
int& bad() {
    int local = 1;
    return local; // dangling: local dies at return
}
std::string_view v;
{ std::string s = "hi"; v = s; } // s dies; v dangles

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits