3Darr

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
592 B
1
Indexable
Never
unsigned ***new_3d_array(unsigned n, unsigned m, unsigned k)
{
    unsigned ***arr1;
    unsigned **arr2;
    unsigned *arr3;

    arr1 = (unsigned ***)malloc(n * sizeof(unsigned **));
    arr2 = (unsigned **)malloc(n * m * sizeof(unsigned *));
    arr3 = (unsigned *)malloc(n * m * k * sizeof(unsigned));

    for (int i = 0; i < n; ++i)
    {
        arr1[i] = arr2 + (i * m);
    }

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            arr1[i][j] = arr3 + j * k;
        }
        arr3 += m * k;
    }

    return arr1;
}