Interview Q&A · C · 12 questions in this topic
Pointers Deep Dive — C Interview Q&A
The heart of C interviews — addresses, dereference, arithmetic, const, double pointers and dangling bugs. Part of Pointers Deep Dive · 202 questions in the C bank.
What is a pointer and what do `&` and `*` mean in each context?
Answer
A pointer is a variable holding a memory address. Symbols change meaning by context: in a declaration, * introduces a pointer (int *p) and in an expression * dereferences one (*p); & in an expression takes an address (&x). So int *p = &x reads: declare p as pointer-to-int, initialize with the address of x.
Stating the declaration-vs-expression split resolves every * / & variant under pressure.
Example
int x = 5; int *p = &x; /* decl *: pointer; expr &: address-of */ int y = *p; /* expr *: dereference -> y == 5 */
What do the `const` positions on pointers mean? How do you read them?
Answer
Read right-to-left: `const int *p` is pointer-to-const (cannot change *p, can reseat p); `int *const p` is const-pointer (cannot reseat p, can change *p); `const int *const p` is neither. The rule for safe APIs: point at const data (const char*) unless the function must mutate through the pointer.
const-correct interfaces push mutation bugs to compile time instead of production.
Example
const int *a; /* pointee const (reseat ok) */ int *const b = NULL; /* pointer const (reseat banned) */ size_t strlen(const char *s); /* classic const-correct API */
💡 Read pointer declarations right-to-left out loud — interviewers accept that as the decoding method.
What is pointer arithmetic and when is it legal?
Answer
Arithmetic (p+1, p++, p-q) moves in units of the pointed-to type and is defined only within (or one past) the same array object. Straying outside is undefined behavior; void* arithmetic is a GNU extension, not standard C (no size to scale by); char* is the byte-level exception.
Modern safe code prefers indexed access and size-checked loops over raw pointer walking.
Key points
- 1Scale — p+1 advances by sizeof(*p) bytes.
- 2Range — inside or one-past the same array only.
- 3Difference — p-q gives element count (ptrdiff_t), same array only.
Example
int a[4] = {10, 20, 30, 40}; int *p = a; /* *(p + 2) == 30; // defined: inside the array */ /* *(p + 100); // undefined behavior */
💡 Within-or-one-past-the-same-array is the complete legality rule.
What is the difference between `int *p`, `int **p`, and `void *p`?
Answer
int *p points at an int (one indirection). int **p points at a pointer-to-int — used for out-params that allocate, argv-style tables, and 2D structures. void* is an untyped address: it holds any object pointer but cannot be dereferenced or arithmetically stepped without casting back to the exact original type.
Each extra * is another hop the reader must track — double pointers deserve a comment stating the ownership story.
Example
int x = 42; void *v = &x; /* type erased */ /* *v; // error: incomplete type */ int *back = (int *)v; /* exact-type round trip */ int allocate(int **out); /* out-param: callee sets *out */
What is a dangling pointer and how do dangling bugs happen?
Answer
A dangling pointer points at storage whose lifetime ended — a destroyed stack frame, freed heap, reallocated buffer, or a returned local address. Using it is undefined behavior that may appear to work until optimization or reuse exposes it.
Common factories: returning &local, use-after-free, realloc without updating all copies, and buffer lifetime shorter than the pointer kept.
Key points
- 1Stack — returning addresses of locals.
- 2Heap — use-after-free, double-free.
- 3Realloc — stale copies after a move.
Example
int *bad(void) { int local = 1; /* return &local; // dangling: frame dies at return */ return NULL; }
What is the difference between `NULL`, `0`, and `nullptr`-style null in C?
Answer
C has no nullptr keyword (that is C++/C23): NULL (from <stddef.h>) is the idiomatic null pointer constant — typically ((void*)0) or plain 0. It converts to any pointer type but is not an object and must never be dereferenced.
Always include the proper header for NULL, compare pointers against NULL (not 0) for readability, and never confuse a null pointer with a null character ('\0').
Example
#include <stddef.h> int *p = NULL; if (p == NULL) { /* ... */ } /* '\0' is a char with value 0 — a different thing entirely */
What is strict aliasing and why does type-punning through pointers break?
Answer
The strict-aliasing rule lets the compiler assume differently-typed pointers do not alias the same memory (with narrow exceptions for char/unsigned char), enabling aggressive load/store optimization. Reinterpreting an int* as float* and reading through it violates the rule — undefined behavior that -fstrict-aliasing builds exploit.
Legal punning uses memcpy, unions (widely supported, standardized in effect via footnotes), or char-pointer inspection.
Example
#include <string.h> float f = 3.14f; int bits; memcpy(&bits, &f, sizeof bits); /* legal punning */ /* int bad = *(int *)&f; // aliasing violation: UB */
💡 Say the fix (memcpy), not just the rule — interviewers want the safe alternative.
How do you read complex declarations (`int *(*f)(int *)`, signal-style)?
Answer
Use the spiral/clockwise rule: start at the identifier, move right through [] and () (functions/arrays bind first), then left through * (pointers) — inside-out, respecting parentheses. int *(*f)(int*) reads: f is a pointer to a function taking int* and returning pointer-to-int.
In real code, typedef the function type once and never make readers decode it twice.
Example
/* f: pointer to function (int*) -> int* */ int *(*f)(int *); typedef int *(*handler_t)(int *); /* name it once */ handler_t h = f;
💡 Typedef function-pointer types at the API boundary — decoding skill is for interviews, typedefs are for codebases.
What is the difference between `*p++`, `(*p)++`, and `*++p`?
Answer
Postfix ++ binds tightest: *p++ is *(p++) — dereference current, then advance the pointer. (*p)++ increments the pointed-to value, pointer unmoved. *++p is *(++p) — advance first, then dereference the new location.
The copy-loop while (*d++ = *s++) depends on exactly this parse — and on the buffers being valid and non-overlapping.
Example
/* while (*dst++ = *src++) ; // copy incl. '\0', then stop */ /* (*p)++ // bump the value */ /* *++p // step forward, then read */
💡 Postfix-binds-tightest explains the whole family in one rule.
What is alignment, and why does misaligned access break (or slow) code?
Answer
Types require addresses that are multiples of their alignment (typically their size: 4-byte int wants 4-aligned addresses). malloc returns maximally-aligned memory; struct layout inserts padding to satisfy every member. Misaligned access is undefined behavior — x86 tolerates it slowly, ARM faults.
Portable code never assumes packing: use memcpy for unaligned buffers, _Alignas/_Alignof (C11) to query and enforce, and offsetof for layouts.
Example
#include <stdalign.h> #include <stddef.h> _Static_assert(_Alignof(int) >= 1, "sanity"); /* safe unaligned read: */ /* memcpy(&value, ptr, sizeof value); */
How do pointers to functions enable polymorphism in C?
Answer
Structs holding function pointers act as manual vtables: each implementation fills the table, callers dispatch through it without knowing the concrete type. Ops tables in drivers, qsort comparators, and state-machine tables are all this pattern.
Pair the table with an explicit context pointer (void* or a struct) since C has no this — and keep handler signatures identical so tables stay assignable.
Example
typedef struct { int (*read)(void *ctx, char *buf, size_t n); void (*close)(void *ctx); } dev_ops_t; /* manual vtable */
Who owns pointed-to memory? (Ownership thinking in C.)
Answer
Every allocation has exactly one owner whose lifetime governs it: the stack owns locals, the caller usually owns out-param buffers, and documented transfer (malloc in callee, free in caller) covers the rest. Non-owners borrow — const pointers, sizes alongside buffers, no freeing.
If you cannot name the owner of a pointer, the design is already broken; encode it in names and comments (borrow vs take vs give).
Key points
- 1Caller-provided — caller allocates and frees (safest).
- 2Callee-allocated — documented transfer, caller frees.
- 3Borrowed — observe only, never free or store past lifetime.
💡 Answer ownership questions by naming the owner — that habit is what interviewers really test.
Recommended resourcesAffiliate links · may earn us a commission