# Initialization of a multidimensional array in C

Array is a collection of same type of elements at contiguous memory location. The lowest address corresponds to the first element while highest corresponds to last element. Array index starts with zero(0) and ends with the size of array minus one(array size - 1). Array size must be integer greater than zero.

Let us see an example,

If array size = 10
First index of array = 0
Last index of array = array size - 1 = 10-1 = 9

Multi-dimensional arrays are arrays of array. The data is stored in tabular form in row major order.

The following is the syntax of multi-dimensional arrays.

type array_name[array_size1][array_size2].......[array_sizeN];

Here,

array_name − Any name given to an array.

array_size − Size of array.

The following is how you can initialize a multi-dimensional array.

type array_name[array_size1][array_size2].......[array_sizeN]; = { {elements} , {elements} , ... , {elements} }

The following is an example of multi-dimensional array.

## Example

Live Demo

#include <stdio.h>
int main () {
int arr[2][3] = { {5,2,3}, {28,8,30}};
int i, j;
for ( i = 0; i < 2; i++ ) {
for ( j = 0; j < 3; j++ )
printf("arr[%d][%d] = %d", i, j, arr[i][j] );
}
return 0;
}

## Output

arr[0][0] = 5
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 28
arr[1][1] = 8
arr[1][2] = 30

In the above program, a two-dimensional array is declared.

int arr[2][3] = { {5,2,3}, {28,8,30}};

The elements of array are printed using nested for loop.

for ( i = 0; i < 2; i++ ) {
for ( j = 0; j < 3; j++ )
printf("arr[%d][%d] = %d", i, j, arr[i][j] );
}
Samual Sam

Learning faster. Every day.