C++ Interview Question

What is the difference between a pointer and a reference in C++?

Updated 2026-08-16 · Beginner friendly
Quick answer

A pointer is a variable that holds a memory address and can be null, reassigned to point elsewhere, and used with pointer arithmetic. A reference is an alias for an existing variable, must be initialised when declared, cannot be null, and cannot be changed to refer to something else. Use references for simple aliasing and pointers when you need to reassign or represent nothing.

Key takeaways
  • A pointer holds an address, can be null, and can be reassigned.
  • A reference is an alias, must be initialised once, and cannot be null or rebound.
  • Prefer references for simple aliasing and pointers when you need reassignment or optional absence.

Pointer

A pointer stores an address. You can make it point somewhere else, set it to nullptr, and follow it with the dereference operator to reach the value.

int x = 10;
int* p = &x;   // p holds the address of x
*p = 20;        // x is now 20
p = nullptr;    // allowed

Reference

A reference is another name for an existing variable. Once bound it always refers to that same variable, so it is safer and cleaner for simple cases.

int x = 10;
int& r = x;   // r is an alias for x
r = 20;        // x is now 20
In the interview

A crisp summary is a pointer can be null and reassigned, a reference must bind once and cannot be null. Saying you prefer references unless you need reassignment or optional absence shows good modern C++ instincts.

Frequently asked questions

Can a reference be null?

No. A reference must bind to a valid object when declared and always refers to it. If you need to represent nothing, use a pointer instead.

What is a dangling pointer?

It is a pointer that still points to memory that has been freed or gone out of scope. Using it causes undefined behaviour.

Want the full C++ guide?

Read every C++ concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the C++ guide All C++ questions