# C - Multidimensional Arrays

### Multi Dimensional Array

Multi-dimensional array is defined using multiple adjacent square brackets. In multi-dimensional array, there is no limit to specify the number of indices. For two-dimensional array require two indices, three-dimensional array require three indices, four-dimensional array require four indices and so on.

### Two-dimensional Array

In C Programming, the two-dimensional array can be represented with two indices or two labels in square brackets. It can be declared as follows

data-typename array-name[size][size];

It has (size*size) elements, which form imaginary grid in which every square is a variable or storage area.

The two-dimensional array can also be represented as rows and columns and it is also known as matrix.

#### Example

int Array[3][3];

The meaning of using two indices is that first index refers the number of rows in the grid whereas second index refers the number of columns in the grid.

### Two-dimensional Array Initialization

The elements of an array may be initialized with values enclosed in curly brace.

#### Example

int array[3][3]={{2,3,4},{9,4,7},{1,7,3}};

Here the braces are nested such that the inner braces enclose each row of the two-dimensional array. In memory, an array cannot be stored in grid instead it can be stored as one-dimensional array. Therefore arrays are stored in row-wise. The above array would be stored as

The multi-dimensional arrays may also be defined without a specific size as same as one dimensional arrays. In case of two-dimensional array, only the left-most index ( the number of rows) is free, and the other dimensions must be given a definite value.

#### Example

int array[][3]={ {2,3,4}, {9,4,7} };

Usually, arrays of more than one dimension are simply handled by nested for loops. Therefore, a two dimensional array may also be initialized as in the following example:

### Example

main () { int i,j; float array[SIZE1][SIZE2]; for (i = 0; i < SIZE1; i++) { for (j = 0; j < SIZE2; j++) { array[i][j] = 0; } } }

In case of three-dimensional array, three nested loops would be needed.

### Accessing of Arrays

To access an element of a two-dimensional array, the correct notation is to enclose each index in a separate pair of square brackets.

int result = array[1][2];

This above statement shows that 3rd element of 2nd row of array is assigned to the given variable.