A multi-dimensional array in C is an array of arrays. It represents a homogeneous collection of data structured across two or more dimensions, physically allocated as a single, contiguous block of memory. C implements multi-dimensional arrays using row-major order, meaning the elements of the rightmost dimension are stored contiguously in memory.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Declaration Syntax
The syntax requires appending additional bracket pairs for each dimension. The dimensions must be integer constant expressions greater than zero.Initialization
Multi-dimensional arrays can be initialized using nested brace-enclosed lists. Because the underlying memory is contiguous, they can also be initialized with a flat list, though nested braces are preferred for readability.Memory Layout and Row-Major Order
In a 2D array declared astype arr[M][N], the compiler maps the two-dimensional indices [i][j] to a one-dimensional memory offset.
The memory address of arr[i][j] is calculated as:
Address = Base_Address + (i * N + j) * sizeof(type)
Because C uses row-major order, iterating through the rightmost dimension (the columns) accesses contiguous memory addresses. Iterating through the leftmost dimension (the rows) jumps memory addresses by a stride equal to the size of the inner array.
Pointer Equivalence and Access
Array subscripting in C is syntactic sugar for pointer arithmetic. For a 2D arrayarr, the expression arr[i][j] is evaluated as:
arrdecays to a pointer to its first element. The first element of a 2D array is a 1D array. Therefore,arris of typetype (*)[N](a pointer to an array ofNelements).arr + iadvances the pointer byiarrays of sizeN.*(arr + i)dereferences this to yield the 1D array at rowi, which then decays into a pointer to its first element (type *).+ jadvances this scalar pointer byjelements.- The final
*dereferences the pointer to yield the actual value.
Passing Multi-Dimensional Arrays to Functions
When passing a multi-dimensional array to a function, the array decays into a pointer to its first element (which is an array of the remaining dimensions). The compiler requires all dimensions except the first to be explicitly defined in the function signature to calculate the correct pointer arithmetic strides.Variable-Length Arrays (VLAs)
Introduced in C99 (and made optional in C11), Variable-Length Arrays allow dimensions to be evaluated at runtime. This is particularly useful for function parameters, allowing dynamic strides without manual pointer arithmetic.Master C with Deep Grasping Methodology!Learn More





