Skip to main content

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.

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.

Declaration Syntax

The syntax requires appending additional bracket pairs for each dimension. The dimensions must be integer constant expressions greater than zero.
// type array_name[dimension1][dimension2]...[dimensionN];
int matrix[3][4];       // 2D array: 3 rows, 4 columns
double tensor[2][3][4]; // 3D array: 2 blocks, 3 rows, 4 columns

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.
// Nested initialization (Preferred)
int grid[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// Flat initialization (Equivalent due to row-major layout)
int flat_grid[2][3] = {1, 2, 3, 4, 5, 6};

// Partial initialization (Unspecified elements are zero-initialized)
int partial_grid[2][3] = {
    {1},       // Row 0: 1, 0, 0
    {4, 5}     // Row 1: 4, 5, 0
};

// Implicit first dimension (Compiler infers '2' based on initialization)
int implicit_grid[][3] = {
    {1, 2, 3},
    {4, 5, 6}
};
Note: Only the first (leftmost) dimension can be inferred. All subsequent dimensions must be explicitly declared.

Memory Layout and Row-Major Order

In a 2D array declared as type 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 array arr, the expression arr[i][j] is evaluated as:
*(*(arr + i) + j)
  1. arr decays to a pointer to its first element. The first element of a 2D array is a 1D array. Therefore, arr is of type type (*)[N] (a pointer to an array of N elements).
  2. arr + i advances the pointer by i arrays of size N.
  3. *(arr + i) dereferences this to yield the 1D array at row i, which then decays into a pointer to its first element (type *).
  4. + j advances this scalar pointer by j elements.
  5. 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.
// The second dimension (4) is mandatory. The first dimension is optional.
void process_matrix(int matrix[][4], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 4; j++) {
            matrix[i][j] *= 2;
        }
    }
}

// Alternative syntax using a pointer to an array
void process_matrix_ptr(int (*matrix)[4], int rows) {
    // Implementation identical to above
}

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.
// Dimensions 'rows' and 'cols' must precede the array in the parameter list
void process_vla_matrix(int rows, int cols, int matrix[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            matrix[i][j] = 0;
        }
    }
}
Master C with Deep Grasping Methodology!Learn More