C++ Easy technical 1 views 1 min read

What is the difference between a pointer and a reference?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of C++ conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

A pointer is a variable that stores an address. It can be null, reassigned and used with pointer arithmetic. A reference is an alias for an existing object: it must be initialised when created, cannot be reseated, and is never null.

int a = 1, b = 2;
int* p = &a;    // p can point elsewhere
*p = 10;        // writes a
p = nullptr;    // allowed

int& r = a;     // r aliases a
r = b;          // assigns b's value into a; rebinding is impossible

Use references for parameters and return values when a value must exist; use pointers when "nothing" is a valid state, for optional output parameters, or for dynamic data structures. Prefer references by default for safety and clearer intent. const T& is the idiomatic way to pass large objects without copying. Note that references are usually implemented as pointers under the hood, and sizeof a reference gives the referenced type's size, not a pointer's.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?