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

Arrays & Array Decay — C Interview Q&A

All C topics

How C arrays really work — decay, sizeof traps, multidimensional layout, and safe passing conventions. Part of Arrays & Strings · 202 questions in the C bank.

What is array-to-pointer decay?

Answer

In most expressions, an array name converts (decays) to a pointer to its first element — losing its size. Exceptions: sizeof, _Alignof, unary & (address of the whole array), and string-literal initializers.

Decay is why functions receive int* plus a separate length: the size information evaporates at the call boundary.

Example

Example.cc
void f(int *p, size_t n); /* size must travel separately */
int a[10];
f(a, sizeof a / sizeof a[0]); /* decay happens at the call */

Why does `sizeof(arr)` work in main but not in a function?

Answer

In the defining scope the name is still an array, so sizeof yields total bytes. Once passed, it decayed to a pointer — sizeof then yields pointer size (8 on 64-bit). The function never saw an array at all.

Conventions: pass (pointer, length) pairs, use sentinel termination where idiomatic, or (C99) sized array parameters for documentation.

Example

Example.cc
/* void g(int a[10]) — 10 is ignored: a is really int* */
void g(int *a, size_t n) { /* use n, never sizeof a */ }

💡 The parameter never was an array is the precise mental model.

How are multidimensional arrays laid out and passed to functions?

Answer

C stores multidimensional arrays contiguously in row-major order. A function parameter must know all dimensions except the first (void f(int a[][COLS])) so it can compute strides — or take a flat pointer plus dimensions and index manually.

Variable-length arrays (C99, optional since C11) allow runtime dimensions with the same row-major math.

Example

Example.cc
#define COLS 4
void sums(int a[][COLS], size_t rows); /* stride needs COLS */
/* flat alternative: void f(int *a, size_t r, size_t c); a[i*c + j] */

💡 All-dimensions-but-first is the parameter rule; row-major is the layout 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