Autocoder

C Pointers Interview Questions

Last updated 2026-11-19

Pointers are the most examined topic in C. Interviewers ask them in three forms: a definition ("what is a dangling pointer"), a trace ("what does this print") and a design question ("write a function that swaps two integers" or "reverse a string in place"). The three are connected by the same few rules about addresses, types and lifetimes.

The answers on this page state the rule, show a short program and give its output, checked with gcc on a 64-bit system where int is 4 bytes and a pointer is 8. Related practice: C programming output questions, C language questions and the C++ view in C++ pointers and references.

TopicsArithmetic, arrays, double pointers, malloc, function pointers
Platform64-bit, int is 4 bytes
LevelEasy to hard

Rules behind almost every pointer question

Typical mistakes interviewers expect you to name

Dereferencing NULL or an uninitialised pointer, using memory after free, freeing twice, forgetting to allocate room for the terminating zero in a string, and returning the address of a local variable. Naming the exact mistake and the fix is worth more than a long definition.

C Pointers Interview Questions: questions with worked answers

  1. 1

    What is a pointer, and what do & and * do?

    easy

    "A pointer is a variable that stores the address of another object." The address-of operator & gives the address of its operand, and the dereference operator * accesses the object at an address. For int x = 5 and int *p = &x, *p is 5, and writing *p = 7 changes x. The pointer type matters: an int* reads four bytes, a char* reads one, which is why you cannot assign between them without a cast.

  2. 2

    What is the output of this swap program, and why does a swap by value fail?

    easy

    It prints 2 1. The function receives the addresses of x and y and swaps the values at those addresses. A version taking int a, int b swaps copies and leaves the caller untouched, because C is strictly pass by value; passing pointers is how C simulates pass by reference.

    c
    void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
    int main() {
        int x = 1, y = 2;
        swap(&x, &y);
        printf("%d %d\n", x, y);   // 2 1
    }
  3. 3

    Explain pointer arithmetic. Predict the output.

    medium

    It prints 30 and 3. Adding n to a pointer advances it by n times the size of the pointed type, so a + 2 reaches the third int. Subtracting two pointers into the same array gives the number of elements between them (type ptrdiff_t). The result is only defined while the pointers stay within the array or one past its end.

    c
    int a[5] = {10, 20, 30, 40, 50};
    printf("%d\n", *(a + 2));        // 30
    printf("%td\n", &a[4] - &a[1]);  // 3
  4. 4

    What is the difference between *p++, (*p)++ and *++p? Predict the output.

    medium

    *p++ reads the value then advances the pointer (postfix binds tighter than *). (*p)++ increments the pointed value and leaves the pointer. *++p advances first and then reads. With a = {10,20,...} and p = a, *p++ prints 10 and p now points at 20; separately (*p)++ prints 10, leaving a[0] as 11 and *p as 11.

    c
    int a[] = {10, 20, 30}; int *p = a;
    printf("%d\n", *p++);   // 10, p now at a[1]
    printf("%d\n", *p);     // 20
    p = a;
    printf("%d\n", (*p)++); // 10, a[0] becomes 11
    printf("%d %d\n", a[0], *p); // 11 11
  5. 5

    Are arrays and pointers the same thing? Show where they differ.

    medium

    No. An array is a block of elements; a pointer is a variable holding an address. The array name converts to a pointer to its first element in most expressions, which is why they look alike, but the conversion does not happen under sizeof or unary &. sizeof(a) is the whole array (20 for five ints), while sizeof of a pointer is 8. You can assign to a pointer but not to an array name, and a function parameter declared as int a[] is actually an int*.

    c
    int a[5];
    int *p = a;
    printf("%zu %zu %zu\n", sizeof(a), sizeof(p), sizeof(&a)); // 20 8 8
  6. 6

    What is a pointer to a pointer, and when do you need one? Predict the output.

    medium

    A double pointer stores the address of another pointer. It is needed when a function must change the caller's pointer, for example to allocate memory or advance through a list head. Dereferencing it once gives the inner pointer, twice gives the final value. In the example, writing through **pp changes a[0] to 99.

    c
    int a[3] = {1, 2, 3};
    int *p = a;
    int **pp = &p;
    **pp = 99;
    printf("%d\n", a[0]);   // 99
  7. 7

    Write a function that allocates memory for the caller using a double pointer.

    medium

    Pass the address of the caller's pointer, allocate inside, and store the result through it. A function taking only int* would modify its own copy and the caller would still hold NULL, leaking the memory. Check malloc for NULL, and the caller must free the block.

    c
    int make(int **out, size_t n) {
        *out = malloc(n * sizeof **out);
        return *out ? 0 : -1;
    }
    int main(void) {
        int *arr = NULL;
        if (make(&arr, 10) == 0) { arr[0] = 1; free(arr); }
    }
  8. 8

    What is a dangling pointer, a wild pointer and a null pointer?

    medium

    A null pointer holds the value NULL (0) and is safe to test but not dereference. A wild pointer is uninitialised, holding an arbitrary address. A dangling pointer once pointed to a valid object that has since been freed or gone out of scope, such as the address of a local returned from a function. All dereferences of wild and dangling pointers are undefined behaviour. Initialise pointers to NULL and set them to NULL after free.

  9. 9

    What does this 2D array code print?

    medium

    It prints 6 and 1, then 24 and 12. m is an array of two arrays of three ints. *(m + 1) is the second row, *(*(m + 1) + 2) is its third element, 6. **m is m[0][0], 1. sizeof(m) is 2*3*4 = 24 and sizeof(m[0]) is one row, 12. A 2D array is not an int**: its rows are contiguous in memory.

    c
    int m[2][3] = {{1,2,3},{4,5,6}};
    printf("%d %d\n", *(*(m + 1) + 2), **m);      // 6 1
    printf("%zu %zu\n", sizeof(m), sizeof(m[0])); // 24 12
  10. 10

    What are function pointers? Show a callback example.

    medium

    A function pointer stores the address of a function with a particular signature, written as return_type (*name)(params). It enables callbacks, dispatch tables and plugins; qsort takes a comparison function this way. Calling it is name(args) or (*name)(args). The comparator for qsort receives const void* and must cast, and should avoid subtraction that can overflow.

    c
    int cmp(const void *a, const void *b) {
        int x = *(const int*)a, y = *(const int*)b;
        return (x > y) - (x < y);
    }
    int main(void) {
        int v[] = {3, 1, 2};
        qsort(v, 3, sizeof v[0], cmp);
    }
  11. 11

    What is the difference between char *s = "abc" and char s[] = "abc"?

    medium

    char s[] creates a modifiable array of four chars initialised from the literal. char *s points to the string literal itself, stored in read-only memory, so writing s[0] = 'X' is undefined behaviour, typically a segmentation fault. Declare it const char *s to make the compiler catch it. sizeof of the array is 4, of the pointer 8.

  12. 12

    What does void * mean, and how do you use it?

    medium

    A void pointer is a generic pointer that can hold the address of any object but cannot be dereferenced or used in arithmetic (in standard C) without converting it. Convert to a typed pointer first: *(int*)v. malloc returns void*, which converts implicitly in C (not in C++). Generic routines such as qsort and memcpy use it, at the cost of type safety.

  13. 13

    Find the bug: a function that returns a local array.

    hard

    The function returns the address of a local array that is destroyed when it returns, so the caller holds a dangling pointer and reading it is undefined behaviour. Fixes: allocate with malloc and document that the caller must free, pass a caller-owned buffer and its size, or make the array static (not thread-safe and shared between calls).

    c
    int *bad(void) { int a[3] = {1, 2, 3}; return a; }   // dangling
    void good(int *out, size_t n) { for (size_t i = 0; i < n; i++) out[i] = (int)i; }
  14. 14

    Implement strlen, strcpy and an in-place string reverse using pointers.

    hard

    strlen walks until the zero byte. strcpy copies bytes including the terminator and returns the destination; the destination must be big enough, which is why strncpy or snprintf is preferred in real code. For reversal, use two pointers swapping toward the middle, on a modifiable array. All three are O(n) time and O(1) extra space.

    c
    size_t my_strlen(const char *s) { const char *p = s; while (*p) p++; return p - s; }
    char *my_strcpy(char *d, const char *s) { char *r = d; while ((*d++ = *s++)); return r; }
    void rev(char *s) {
        char *i = s, *j = s + my_strlen(s);
        while (i < --j) { char t = *i; *i++ = *j; *j = t; }
    }

Frequently asked questions

Why is a pointer 8 bytes in my output but 4 in my book?

The size depends on the platform: 4 bytes on 32-bit systems and 8 on 64-bit. Old textbooks often use 16-bit Turbo C where int is 2 bytes.

Is NULL always zero?

The null pointer constant compares equal to 0 in source code, but its internal representation need not be all zero bits on every platform.

What is the difference between malloc and calloc?

malloc leaves memory uninitialised; calloc zeroes it and takes a count and a size, checking the multiplication for overflow.

Where can I find more C questions?

Try C language questions for storage classes, structs and preprocessor topics.

Related guides

More in Programming Language Interview Questions by Language

See all in this section