As mentioned earlier, in this particular case, doing anything other than a static assignment [6][22][7] would be a waste of time. If you really want to dynamically allocate an array using malloc :
/* Suppose that you want a [5][22][6] */ int main() { int i,j,k; int ***boo; int d_1,d_2,d_3; d_1=5; d_2=22; d_3=6; /* +------------------------------------------+ | For each dimension, a malloc is needed | +------------------------------------------+ */ boo = malloc(d_1*sizeof(int*)); for (i=0;i<d_1;i++) { boo[i] = malloc(d_2*sizeof(int*)); for (j=0;j<d_2;j++) { boo[i][j] = malloc(d_3*sizeof(int*)); for (k=0;k<d_3;k++) { boo[i][j][k] = i+j*k; } } } /* +----------------------+ | Testing the values | +----------------------+ */ for (i=0;i<d_1;i++) { for (j=0;j<d_2;j++) { for (k=0;k<d_3;k++) { printf("%d ",boo[i][j][k]); } printf("\n"); } } return 0; }
That would essentially do the trick. This can be useful if you have more data.
Remember to free memory with free()
source share