What is a "multilevel" array (array of pointers), and how does it differ from a true 2D array?
A multilevel array like int *A[N] is an array of N pointers, each pointing to its own separate block — not one contiguous rectangle the way int A[N][M] is.
* int *A[N] is N independent pointers to separate, possibly ragged blocks — and a slot can be NULL. *
C's int matrix[3][4] is a true 2D array: 12 ints in one contiguous, row-major block, and the compiler computes any element's address arithmetically. A multilevel array is different — it's literally an array of pointers:
int *A[N]; // N pointers; each A[i] points somewhere else
Key differences and consequences:
- Layout: the rows live in scattered allocations, not one block.
Aitself only stores the N pointers. - Ragged rows: because each pointer is independent, the pointed-to arrays can have different lengths — perfect for jagged data (e.g. a list of strings of varying length).
- NULL entries: a pointer slot can be
NULLto mark "no row here" (this is exactly howargvand achar *args[]list use a trailingNULLsentinel). - Extra indirection: reaching
A[i][j]costs an extra memory access — first load the pointerA[i], then index into it — whereas a true 2D array needs none.
// Array of strings: classic multilevel array
char *names[] = {"Alice", "Bob", "Carol", NULL};
for (char **p = names; *p; p++)
printf("%s\n", *p);
Tip: in a declaration, read right-to-left with cdecl — int *A[N] is "A is an array of N pointers to int", not "pointer to an array". Use https://cdecl.org/ when in doubt.
Go deeper:
Pointer (computer programming) — Wikipedia — how arrays of pointers build jagged and indirect structures.