Interview Q&A · C · 12 questions in this topic

Pointers Deep Dive — C Interview Q&A

All C topics

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

Example.cc
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

Example.cc
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

  1. 1Scale — p+1 advances by sizeof(*p) bytes.
  2. 2Range — inside or one-past the same array only.
  3. 3Difference — p-q gives element count (ptrdiff_t), same array only.

Example

Example.cc
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.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits