What is Multi-Dimensional Array in C?
In C programming, Multi-Dimensional Arrays refer to as array of arrays. This could be of 2D or 3D (two-dimensional or three-dimensional) Array. These arrays are stored in the form of a table (with rows and columns).
The general form of multi-dimensional array:
data-type array_name [size1][size2]...[sizeN];
The data type must be a valid C data type, a unique name must be specified to each array and the size must be of an integer constant.
Declaration of 2d and 3d Array in C.
int arr1[3][4]; //2D Array
OR
int arr2[3][4][3]; //3D Array
Initialization of multidimensional array.
We can initialize a Multi Dimensional Array (two-dimensional or three-dimensional) in the following ways in C.
1. Initialization of 2d Array
//Method1
//initializing with 5 rows and 3 columns
int arr[5][3] = {
{1, 2, 3},
{2, 3, 4},
{3, 4, 5},
{4, 5, 6},
{7, 8, 9}
};
OR
//Method2
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
2. Initialization of 3d Array
//Method1
int arr[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
OR
//method2
int arr[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};
Accessing Multi-Dimensional Array Elements in C
Accessing 2d Array:
An array can be accessed by using a specific index number. It can be achieved by placing the particular index number within the brackets [][]. Such as:
arr[2][3]; //the 4th element of the third row is accessed
arr[3][2]; //the 3rd element of the fourth row is accessed
OR
int val = arr[2][3]; // assigning the element to val
Accessing 3d Array:
Accessing 3-dimensional array is also as same as a two-dimensional array, the only difference is that in 3d array we have to specify 3 subscripts with a required index number.
Example of two -dimensional Array in C:
Displaying the elements with index number from 2D Arrays.
#include <stdio.h>
int main()
{
//Array with 4 rows and 2 columns
int arr[4][2] = {
{ 0, 1 },
{ 2, 3 },
{ 4, 5 },
{ 6, 7 }
};
int i, j;
//Displaying the elements
printf("Elements with index numbers:\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 2; j++)
{
printf("a[%d][%d] = %d\n", i, j, arr[i][j]);
}
}
return 0;
}
Output: After executing the above code, the following result will be displayed.
arr[0][0]: 0
arr[0][1]: 1
arr[1][0]: 2
arr[1][1]: 3
arr[2][0]: 4
arr[2][1]: 5
arr[3][0]: 6
arr[3][1]: 7
Example of three-dimensional Array in C:
Displaying the elements with index number from 3D Arrays.
#include <stdio.h>
int main()
{
int arr[2][3][2] = {
{ {0,1}, {2,3}, {4,5} },
{ {6,7}, {8,9}, {10,11} }
};
int i, j, k;
// Display with index number
printf("Elements with index number: \n");
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 3; ++j)
{
for (k = 0; k < 2; ++k)
{
printf("Element at [%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
}
}
}
return 0;
}
Output: After executing the above code, the following result will be displayed.
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 1
Element at arr[0][1][0] = 2
Element at arr[0][1][1] = 3
Element at arr[0][2][0] = 4
Element at arr[0][2][1] = 5
Element at arr[1][0][0] = 6
Element at arr[1][0][1] = 7
Element at arr[1][1][0] = 8
Element at arr[1][1][1] = 9
Element at arr[1][2][0] = 10
Element at arr[1][2][1] = 11
As we saw the examples of 2D and 3D arrays, in a similar we can create any number dimension as required. However, the most used multidimensional array is a two-dimensional array.