C++ for Embedded · Interview question

What's the difference between a reference and a pointer?

A strong answer

A reference is an alias, another name for an existing object, while a pointer is a distinct object that stores an address. Three concrete differences follow: a reference must be initialized when declared and binds to a valid object (there's no null reference), it can't be reseated to refer to a different object after binding, and you use it with no dereference syntax (it is the object). A pointer can be null, can be repointed anytime, can be uninitialized, and requires explicit */&. Under the hood a reference is typically implemented as a pointer, but the language's guarantees mean a function taking a reference doesn't need to null-check or worry the caller forgot to initialize. The practical guidance: default to references, drop to pointers when you need the capabilities references deliberately lack.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

References vs Pointers

References are aliases that can't be null and can't be reseated. When to reach for a reference, when you still need a pointer, and why const& is the default way to pass objects.

More References vs Pointers questions

Browse all 472 interview questions