Kawsar_Ahmed Kawsar_Ahmed - 12 days ago 5
C Question

How the multidimensional array works?

In this program an array has 3 sizes. First size refers to row, second refers to column, but what the third do? I have another question, array refers

%s
to print.But in this program array printed by
%d
.Since array assign to
x
value. Which is
int
data type, but we print array. So how it would be
%d
?

#include<stdio.h>

int main(void) {
char array[3][3][3];
int i,j,k;

int x = 1;

for(i=0; i<3; i++){
for(j=0; j<3; j++){
for(k=0; k<3; k++){
array[i][j][k] = x;
printf("%d\n", array[i][j][k]);
x++;
}

}

}
return 0; }

Answer

char[3] filled with ones would look like:

+---+---+---+
| 1 | 1 | 1 |
+---+---+---+

char[3][3] filled with ones would look like:

+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+

char[3][3][3] filled with ones would be three above square arrays one over one. There will be a cube.

enter image description here

On the above picture, each small cube will represent single cell so there are 27 cells in total.

I'm glad you didn't ask about char[3][3][3][3].

Comments