Key takeaways
- Pointer questions decide most C, embedded and systems interviews — expect pointer arithmetic, arrays vs pointers and
constplacement. - Interviewers love "what is the output?" snippets and bug-spotting: dangling pointers, returning a local's address,
sizeofon array parameters, memory leaks. - For experienced roles, know function pointers, double pointers,
void *andrestrict, and how to find bugs with Valgrind or AddressSanitizer.
Pointers are the topic that separates C candidates in interviews — for freshers, embedded and firmware roles, and systems programming. Below are the C pointer interview questions asked most often, each with a short answer and code you can run. The full C interview questions page covers the rest of the language, and freshers should also read C interview questions for freshers.
Pointer basics
What is a pointer, and what do & and * do?
A pointer is a variable that stores the memory address of another object. &x gives the address of x; *p dereferences p to read or write the value it points to.
#include <stdio.h>
int main(void) {
int x = 10;
int *p = &x; // p holds the address of x
*p = 20; // write through the pointer
printf("%d %d\n", x, *p); // 20 20
return 0;
}Write a function that swaps two integers.
C passes arguments by value, so the function must receive pointers to change the caller's variables:
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
// usage: swap(&x, &y);How does pointer arithmetic work?
Adding n to a pointer moves it by n * sizeof(p) bytes, not n bytes. That's why p + 1 on an int * moves 4 bytes on most systems, and why arr[i] is defined as (arr + i).
int arr[] = {10, 20, 30, 40};
int *p = arr; // arrays decay to a pointer to the first element
printf("%d\n", *(p + 2)); // 30
printf("%d\n", 2[arr]); // 30 — legal, because a[b] is *(a + b)
printf("%td\n", &arr[3] - &arr[0]); // 3 — pointer difference counts elementsArrays vs pointers
What is the difference between an array and a pointer?
An array is a block of elements with a fixed size; a pointer is a variable holding an address. Arrays "decay" to a pointer to their first element in most expressions, but they are not the same:
int arr[5];
int *p = arr;
printf("%zu %zu\n", sizeof arr, sizeof p); // 20 8 (on a 64-bit system)
// arr = p; // ERROR: an array name is not assignable
p = arr + 1; // fine: p is a variableWhy does sizeof give the wrong array length inside a function?
An array parameter is really a pointer — void f(int a[]) means void f(int *a) — so sizeof(a) returns the size of a pointer. Always pass the length separately:
#include <stdio.h>
void print_len(int a[]) {
printf("%zu\n", sizeof(a) / sizeof(a[0])); // 2 on 64-bit, not 5! (8 / 4)
}
int main(void) {
int arr[5] = {0};
printf("%zu\n", sizeof(arr) / sizeof(arr[0])); // 5
print_len(arr);
return 0;
}What is the difference between char *s = "hi" and char s[] = "hi"?
char *s = "hi";points to a string literal, usually in read-only memory. Modifying it (s[0] = 'H') is undefined behaviour — often a crash. Declare itconst char *.char s[] = "hi";creates a modifiable array of 3 chars (including\0) initialised with a copy.
const, double and void pointers
Explain const int *p, int *const p and const int *const p.
Read the declaration right to left:
const int *p— pointer to a const int: you can't change*p, but you can movep.int *const p— const pointer to an int: you can change*p, but notp.const int *const p— neither can change.
int a = 1, b = 2;
const int *p1 = &a; /* *p1 = 5; error */ p1 = &b; /* ok */
int *const p2 = &a; *p2 = 5; /* ok */ /* p2 = &b; error */When do you need a pointer to a pointer?
When a function must change the caller's pointer — for example allocating memory for it, or inserting at the head of a linked list:
#include <stdlib.h>
struct Node { int val; struct Node *next; };
void push_front(struct Node **head, int val) {
struct Node *n = malloc(sizeof *n);
if (!n) return;
n->val = val;
n->next = *head;
*head = n; // updates the caller's head pointer
}
// usage: struct Node *head = NULL; push_front(&head, 3);What is a void * pointer?
A generic pointer that can hold the address of any object type. It can't be dereferenced or used in arithmetic directly — cast it to a concrete type first. malloc returns void *, and generic APIs such as qsort and memcpy use it.
Function pointers
What is a function pointer? Show a callback with qsort.
A function pointer stores the address of a function, so code can choose what to call at run time — callbacks, dispatch tables and plugin interfaces.
#include <stdio.h>
#include <stdlib.h>
int cmp_desc(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
return (y > x) - (y < x); // avoids overflow from y - x
}
int add(int a, int b) { return a + b; }
int main(void) {
int nums[] = {5, 1, 9, 3};
qsort(nums, 4, sizeof nums[0], cmp_desc);
for (int i = 0; i < 4; i++) printf("%d ", nums[i]); // 9 5 3 1
printf("\n");
int (*op)(int, int) = add; // pointer to a function taking two ints
printf("%d\n", op(2, 3)); // 5
return 0;
}Memory bugs interviewers ask you to spot
What are NULL, dangling and wild pointers?
- NULL pointer: deliberately points to nothing. Check it before dereferencing.
- Dangling pointer: points to memory that has been freed or has gone out of scope.
- Wild pointer: an uninitialised pointer holding a garbage address.
What is wrong with this function?
int *make_value(void) {
int x = 42;
return &x; // BUG: x lives on the stack and is destroyed on return
}The returned pointer is dangling. Using it is undefined behaviour. Fix it by returning the value itself, by using malloc (and documenting that the caller must free), or by having the caller pass a buffer in.
What is a memory leak, and how do you avoid common malloc mistakes?
A leak is allocated memory that is never freed because the last pointer to it was lost. Common mistakes:
char *buf = malloc(100);
buf = malloc(200); // LEAK: the first 100 bytes are unreachable
char *s = malloc(strlen(src)); // BUG: no room for '\0' — use strlen(src) + 1
free(p);
free(p); // BUG: double free — set p = NULL after free
p = realloc(p, n); // BUG: if realloc fails, the original block leaksSafe realloc pattern:
int *tmp = realloc(arr, new_n * sizeof *arr);
if (tmp == NULL) { /* handle error; arr is still valid */ }
else arr = tmp;Tools to mention: gcc -fsanitize=address -g (AddressSanitizer) and Valgrind (valgrind --leak-check=full ./prog) catch leaks, use-after-free and buffer overflows immediately.
What does restrict mean?
restrict (C99) promises the compiler that, for the pointer's lifetime, no other pointer accesses the same object. That allows more aggressive optimisation — memcpy uses it, which is why overlapping copies must use memmove. Breaking the promise is undefined behaviour.
Keep practising
- 100+ C questions with answers: C interview questions
- Practise output questions: free C quiz
- Starting out? C interview questions for freshers