The following function allocates the memory for your dynamic array. elem_size
is the size for each element, n
refers to the size of the first dimension, the other two dimensions are sized 128
.
void* create_3Darray(size_t elem_size, size_t n) {
return malloc(elem_size * n * 128 * 128);
}
Usage:
int ***arr = create_3Darray(sizeof(int), 256);
arr[2][5][12] = 12;
You can substitute malloc
with calloc
to initialize the elements to 0
, otherwise the array might be filled with random values.
Also you should be careful not to read/write over the arrays/dimensions bounds.