I am having some difficulties in understanding the code below. First I don't understand, is (char***) a cast to a triple pointer to a char? While I am am somewhat familiar with single pointers, these triples are beyond my current understanding and I would like to know, what is the point? The second question is, why have 3 initializations, one for "a", one for a[0], and one for a[0][0]? while I somewhat understand that ***a is a/can be a 3D array, I don't understand how having a block of memory of d1*d2*d3*size at a[0][0] is needed, why not just have it the "d3" so it would be like "depth"? Any clue would be much appreciated...
void ***array3(long d1, long d2, long d3, long size)
{
char ***a;
long i,j;
a = (char ***) my_malloc(d1*sizeof(char *));
a[0] = (char **) my_malloc(d1*d2*sizeof(char *));
a[0][0] = (char *) my_malloc(d1*d2*d3*size);
for (i=1;i<d1;i++) {
a[i] = a[i-1] + d2;
a[i][0] = a[i-1][0] + d2*d3*size;
}
for (i=0;i<d1;i++)
for (j=1;j<d2;j++)
a[i][j] = a[i][j-1] + d3*size;
return (void ***) a;
}
where my_malloc is:
void *my_malloc(size_t size)
{
void *ptr;
if (ptr = (void *) malloc(size))
return ptr;
fprintf(stderr,"Memory allocation failed\n");
exit(1);
}
Thank you,
Brian