Certainly! Here is an example program that demonstrates how to declare, initialize, and access a multi-dimensional array in C:
#include <stdio.h>
int main(void) {
// Declare a multi-dimensional array of integers with size 2x3x4
int array[2][3][4];
// Initialize the array with values
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
array[i][j][k] = i + j + k;
}
}
}
// Access and print the values of the array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
}
}
}
return 0;
}
This program declares a multi-dimensional array of integers called array
with size 2x3x4 (2 layers, 3 rows, and 4 columns), initializes the array with the sum of the indices, and then prints the values of the array to the console.
Here is an example of the output of this program:
array[0][0][0] = 0
array[0][0][1] = 1
array[0][0][2] = 2
array[0][0][3] = 3
array[0][1][0] = 1
array[0][1][1] = 2
array[0][1][2] = 3
array[0][1][3] = 4
array[0][2][0] = 2
array[0][2][1] = 3
array[0][2][2] = 4
array[0][2][3] = 5
array[1][0][0] = 1
array[1][0][1] = 2
array[1][0][2] = 3
array[1][0][3] = 4
array[1][1][0] = 2
array[1][1][1] = 3
array[1][1][2] = 4
array[1][1][3] = 5
array[1][2][0] = 3
array[1][2][1] = 4
array[1][2][2] = 5
array[1][2][3] = 6
0 Comments