Autocoder

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.

TopicsPointers, references, const, lifetime
FormatWorked answers with short code
LevelEasy 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

C++ Pointers and References Interview Questions: questions with worked answers

  1. 1

    What is a dangling pointer and how does it arise?

    easy

    It 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.

    cpp
    int* 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. 2

    Can a reference be null, and can you reseat it?

    easy

    No 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).

    cpp
    int 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 1
  3. 3

    Predict the output of the following pointer-to-pointer code.

    medium

    It 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*&.

    cpp
    void 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. 4

    What is the output of this pointer arithmetic code?

    medium

    It 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.

    cpp
    int 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";      // 3
  5. 5

    Explain const int*, int* const and const int& and give a case where each is the right choice.

    medium

    const 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. 6

    Does binding a const reference to a temporary extend its life?

    medium

    Yes, 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.

    cpp
    const 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 line
  7. 7

    When would you pass by pointer, by reference or by value?

    medium

    Pass 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. 8

    What is the difference between nullptr, NULL and 0?

    medium

    nullptr (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. 9

    What is a function pointer and how does it differ from std::function?

    medium

    A 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.

    cpp
    int 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 6
  10. 10

    What is the output, and what is wrong with this code?

    hard

    The 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.

    cpp
    std::vector<int> v = {1, 2, 3};
    int& r = v[0];
    v.push_back(4);          // may reallocate
    std::cout << r;          // undefined behaviour
  11. 11

    What is the difference between a pointer to an array and an array of pointers?

    medium

    int (*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.

    cpp
    int 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 types
  12. 12

    How does a char array differ from a pointer to a string literal?

    medium

    Both 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. 13

    What is void* and why is it discouraged in C++?

    medium

    void* 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. 14

    Explain pointer aliasing and what restrict-style assumptions mean in C++.

    hard

    Two 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 defined
  15. 15

    What is the difference between "delete p" and "delete[] p"?

    easy

    delete 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

See all in this section