new_4d_array

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
659 B
9
Indexable
Never
unsigned**** new_4d_array(unsigned n,unsigned m,unsigned k,unsigned l){
    unsigned**** ln;
    unsigned*** lm;
    unsigned** lk;
    unsigned* ll;

    ln = (unsigned****) malloc(n*sizeof(unsigned***));
    lm = (unsigned***) malloc(n*m*sizeof(unsigned**));
    lk = (unsigned**) malloc(n*m*k*sizeof(unsigned*));
    ll = (unsigned*) malloc(n*m*k*l*sizeof(unsigned));

    for(int i=0; i<n; i++){
        ln[i] = lm + i*m;
        for(int j=0; j<m; j++){
            lm[i*m+j] = lk + (i*m+j)*k;
            for(int t=0; t<k; t++){
                lk[(i*m+j)*k+t] = ll + ((i*m+j)*k+t)*l;
            }
        }
    }

    return ln;
}