C++ Pointers and References Interview Questions
Last updated 2026-11-19
Pointers and references are the part of C++ where interviewers separate people who have written the language from people who have read about it. The questions are short, the code is two or three lines, and the answer depends on precise rules: what binds to what, what a temporary lives for, and what the compiler is allowed to assume.
This page covers declarations, const placement, pointer arithmetic, references to temporaries, dangling pointers and the pass-by-pointer versus pass-by-reference decision. Each answer states the rule and the symptom you would see. Related pages: C pointers questions for the C-level view and C++ memory management for ownership.
| Topics | Pointers, references, const, lifetime |
|---|---|
| Format | Worked answers with short code |
| Level | Easy to hard |
A mental model that answers most questions
A pointer is a variable that stores an address. A reference is another name for an existing object, and at the machine level compilers implement it as an address too, but the language forbids you from treating it as one. Every question below reduces to one of three checks: what does this name refer to, is that object still alive, and does this expression allow modification.
Typical mistakes
- Returning a reference or pointer to a local variable.
- Holding an iterator, reference or pointer into a vector across a push_back.
- Confusing "const int*" with "int* const".
- Assuming a null pointer dereference reliably crashes. It is undefined behaviour.
- Using sizeof on a pointer parameter and expecting the array size.
C++ Pointers and References Interview Questions: questions with worked answers
1 What is a dangling pointer and how does it arise?
easyIt is a pointer holding the address of an object whose lifetime has ended. Common causes: returning the address of a local, using a pointer after delete or free, and keeping a pointer into a container after it reallocates. Dereferencing it is undefined behaviour: it may appear to work, print garbage or crash. Setting a pointer to nullptr after delete helps with simple cases but does not fix other aliases, so prefer smart pointers.
cppint* bad() { int x = 7; return &x; } // dangling int main() { int* p = new int(5); delete p; // *p = 1; undefined behaviour p = nullptr; // safe to test afterwards }2 Can a reference be null, and can you reseat it?
easyNo to both in well-formed code. A reference must be initialised and always refers to the same object. Assigning to it assigns to the referred object, it does not rebind it. You can form a null reference only through undefined behaviour such as dereferencing a null pointer. If you need an optional or reseatable alias, use a pointer or std::reference_wrapper (or std::optional<T&> style wrappers).
cppint a = 1, b = 2; int& r = a; r = b; // a is now 2; r still refers to a std::cout << a << " " << (&r == &a) << "\n"; // 2 13 Predict the output of the following pointer-to-pointer code.
mediumIt prints 10 10 20. Passing int* by value gives the function a copy of the pointer, so p = &g inside f does not change the caller's a, but writing through it changed x. The pointer b was passed by reference (int*&), so q = &g really repointed b at y. To let a function change where the caller's pointer points, pass int** or int*&.
cppvoid f(int* p, int*& q, int& g) { *p = 10; // changes x p = &g; // local copy only q = &g; // repoints the caller's b } int main() { int x = 0, y = 20; int* a = &x; int* b = &x; f(a, b, y); std::cout << x << " " << *a << " " << *b << "\n"; // 10 10 20 }4 What is the output of this pointer arithmetic code?
mediumIt prints 30 and then 3. Adding n to an int* advances by n elements, not n bytes, so p + 2 points at the third element. Subtracting two pointers into the same array gives the element count as ptrdiff_t. Pointer arithmetic is only defined within an array or one past its end; going beyond that, even without dereferencing, is undefined behaviour.
cppint a[] = {10, 20, 30, 40, 50}; int* p = a; std::cout << *(p + 2) << "\n"; // 30 int* q = &a[4] - 1; // points at 40 std::cout << q - p << "\n"; // 35 Explain const int*, int* const and const int& and give a case where each is the right choice.
mediumconst int* is a pointer to read-only data: use it for input parameters that are arrays or optional values. int* const is a fixed pointer to modifiable data, for example a handle member you never rebind. const int& binds to lvalues and temporaries without copying, so it is the default for passing larger objects that you only read. A const reference parameter can accept a literal, "f(5)", but a non-const int& cannot, because a temporary will not bind to a non-const lvalue reference.
6 Does binding a const reference to a temporary extend its life?
mediumYes, for a local named const reference: the temporary lives as long as the reference. The extension does not carry through a function call. A function that takes a const reference and returns it hands back a reference to a temporary that dies at the end of the full expression, so the second declaration below dangles. Range-based for over a member of a temporary, such as for (auto x : make().items()), is a related pitfall before C++23.
cppconst std::string& a = std::string("hi"); // life extended: ok const std::string& g(const std::string& s) { return s; } const std::string& b = g("tmp"); // dangling after this line7 When would you pass by pointer, by reference or by value?
mediumPass small trivially copyable types (int, double, pointers, string_view, span) by value. Pass larger objects you only read by const reference. Pass by non-const reference when the function must modify the argument and the argument is required. Pass a pointer when "no object" is a valid input, or when the function stores the address. In modern code, use smart pointers to show ownership transfer, and use out-parameters sparingly, preferring return values or a struct return with structured bindings.
8 What is the difference between nullptr, NULL and 0?
mediumnullptr (C++11) has its own type, std::nullptr_t, and converts to any pointer type but not to an integer. NULL is a macro that is 0 or 0L, so overloading f(int) and f(char*) makes f(NULL) call the integer version, or be ambiguous. Templates also deduce 0 as int, losing pointer-ness. Always use nullptr.
9 What is a function pointer and how does it differ from std::function?
mediumA function pointer stores the address of a function with a given signature, for example int (*op)(int, int). It cannot hold a capturing lambda, has no state and is as cheap as a pointer. std::function is a type-erased wrapper that holds any callable with a matching signature (function, lambda with captures, functor), at the cost of a possible heap allocation and an indirect call. Use a template parameter for the fastest generic callback, a function pointer for C APIs and std::function when you need to store callables of different types.
cppint add(int a, int b) { return a + b; } int (*op)(int, int) = add; std::function<int(int,int)> f = [k = 3](int a, int b){ return a + b + k; }; std::cout << op(1, 2) << " " << f(1, 2); // 3 610 What is the output, and what is wrong with this code?
hardThe behaviour is undefined, so there is no valid output. vector::push_back may reallocate, which invalidates r, a reference to the first element, and then r is read. It often prints the right value with small vectors and fails with a reallocation, which makes the bug intermittent. Fix by indexing again after the push, reserving capacity first, or copying the value instead of holding a reference.
cppstd::vector<int> v = {1, 2, 3}; int& r = v[0]; v.push_back(4); // may reallocate std::cout << r; // undefined behaviour11 What is the difference between a pointer to an array and an array of pointers?
mediumint (*p)[5] is a pointer to an array of five ints, so p + 1 advances by five ints. int* p[5] is an array of five int pointers. The declaration is read inside out: parentheses bind the star to the name first. A 2D array int a[3][4] decays to int (*)[4], which is why a function taking it must specify the column count and why int** is not compatible with it.
cppint a[3][4]; int (*row)[4] = a; // pointer to array of 4 ints int* ptrs[4]; // array of 4 int pointers // int** pp = a; // error: different types12 How does a char array differ from a pointer to a string literal?
mediumBoth print the text, but the two declarations differ. char s[] = "abc" creates a modifiable array of four chars (including the terminator), so s[0] = 'X' is fine and sizeof(s) is 4. const char* t = "abc" points at a string literal in read-only storage, so modifying t[0] is undefined behaviour and sizeof(t) is the pointer size. In C++ the literal has type const char[4], and binding it to a plain char* is ill-formed since C++11.
13 What is void* and why is it discouraged in C++?
mediumvoid* holds the address of any object type but can be neither dereferenced nor incremented, and loses all type information. Converting back needs an explicit static_cast to the original type, and a wrong cast is undefined behaviour with no check. It survives in C interfaces such as qsort callbacks and thread entry points. In C++ prefer templates, virtual functions or std::variant and std::any, which keep type safety.
14 Explain pointer aliasing and what restrict-style assumptions mean in C++.
hardTwo pointers alias when they refer to the same object. The compiler must assume that writing through one pointer may change what another pointer reads unless the types differ (the strict aliasing rule) or the code proves otherwise. The strict aliasing rule says accessing an object through an unrelated type, such as reading a float as int via a cast pointer, is undefined behaviour except through char, unsigned char or std::byte. To reinterpret bytes use memcpy or std::bit_cast (C++20), which compile to the same code without breaking the rule.
cpp#include <bit> #include <cstdint> float f = 1.0f; // uint32_t u = *reinterpret_cast<uint32_t*>(&f); // UB uint32_t u = std::bit_cast<uint32_t>(f); // C++20, well defined15 What is the difference between "delete p" and "delete[] p"?
easydelete destroys one object and frees its memory; delete[] destroys every element of an array allocated with new[], calling each destructor, and frees the whole block. Using the wrong form is undefined behaviour: it may leak, corrupt the heap or crash. Deleting a null pointer is a safe no-op. Prefer std::vector or std::make_unique<T[]>(n) so the matching release is automatic.
Frequently asked questions
Are references implemented as pointers?
Usually yes at the machine level, but the standard does not require it, and a reference can be optimised away entirely. Do not rely on its size or address.
Why use a reference instead of a pointer?
References cannot be null, need no dereference syntax and signal that the argument is required. Pointers are for optional values, reseating and ownership APIs.
Is null pointer dereference always a crash?
No. It is undefined behaviour. The compiler may remove code that follows it, so you cannot count on a segmentation fault.
What should I read next for memory questions?
Continue with C++ memory management questions and smart pointers.
Related guides
More in Programming Language Interview Questions by Language
- C++ Smart Pointers Interview Questions
- C++ STL Interview Questions
- C++ Templates Interview Questions
- C++ Virtual Functions Interview Questions
- Core Java Interview Questions for Freshers
- Entity Framework Core Interview Questions
- Java 8 Streams and Lambdas Interview Questions
- Java Coding Interview Questions
- Java Collections Interview Questions
- Java Concurrency Utilities Interview Questions
- Java Exception Handling Interview Questions
- Java Garbage Collection Interview Questions