In C, I want to loop through an array in this order

for(int z = 0; z < NZ; z++)
for(int x = 0; x < NX; Z++)
for(int y = 0; y < NY; y++)
3Darray[x][y][z] = 100;


How do I create this array in such a way that 3Darray[0][1][0] comes right before 3Darray[0][2][0] in memory?

I can get an initialization to work that gives me "z-major" ordering, but I really want a y-major ordering for this 3d array

The code I have been trying to use follows..I have tried swithcing these dimensions around here, but more or less always get the same results. When i do a print in a Z-X-Y loop it never comes out contiguous. This code also doesnt seem to work in all cases. I tried to make a 2-3-4 dimension matrix, and it was not working correctly

Code:
char *space; 
char ***Arr3D; 
int y, z; 
ptrdiff_t diff; 
 
 
 
space = malloc(X_DIM * Y_DIM * Z_DIM * sizeof(char)) 
 
Arr3D = malloc(Z_DIM * sizeof(char **)); 
 
for (z = 0; z < Z_DIM; z++) 
{ 
    Arr3D[z] = malloc(Y_DIM * sizeof(char *)); 
 
    for (y = 0; y < Y_DIM; y++) 
    { 
        Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM); 
    } 
}