Multi-Dimensional array in C programming; In this tutorial, you will learn how to create and to use multidimensional arrays in c programming with the help of examples.
C Programming (3D) Multidimensional Arrays
- Definition of Multi Dimensional Array in C
- Declaration of Multi Dimensional Array in C
- Initialization of Multi Dimensional Array in C
- Example 1 – Multi Dimensional Array in C
Definition of Multi Dimensional Array in C
A three-dimensional (3D) or Multi dimensional array is an array of arrays of arrays.
Let’s understand by example; An array can contain two, three, or even ten or more dimensions.
Note that:- The maximum dimensions a C program can have to depend on which compiler is being used.
Declaration Multi Dimensional Array in C
You can use the following syntax to declare a multi-dimensional array in the c programming language; as shown below:
type array_name[d1][d2][d3][d4]………[dn];
Let’s see the following example for how to declare multi-dimensional array in c programming; as shown below:
int table[5][5][20];
Initialization of Multi Dimensional Array
Use the following example for initializing a three-dimensional or multi dimensional array in a similar way to a two-dimensional array; as shown below:
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
Example 1 – Multi Dimensional Array in C
// C Program to store and print 12 values entered by the user #include <stdio.h> int main() { int test[2][3][2]; printf("Enter 12 values: \n"); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { scanf("%d", &test[i][j][k]); } } } // Printing values with proper index. printf("\nDisplaying values:\n"); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]); } } } return 0; }
Output:
Enter 12 values: 1 2 3 4 5 6 7 8 9 10 11 12 Displaying Values: test[0][0][0] = 1 test[0][0][1] = 2 test[0][1][0] = 3 test[0][1][1] = 4 test[0][2][0] = 5 test[0][2][1] = 6 test[1][0][0] = 7 test[1][0][1] = 8 test[1][1][0] = 9 test[1][1][1] = 10 test[1][2][0] = 11 test[1][2][1] = 12