I have a huge code using a 3D array managed with pointers. Something like:
int *** tab;
tab = malloc(m*sizeof(int**));
for(i= 1..n) tab[i] = malloc(n*sizeof(int*));
... etc...
and later the elements are accessed with:
tab[i][j][k] = ...
But because of specific issues with this structure, I would like to declare tab as a contiguous array instead but still use the syntax with 3 brackets in the code. The compiler will internally replace them like this:
tab[i][j][k] = ... => tab[i*m*n+j*m+k] = ...
So the array is accessed with only one pointer dereference. I'd like not to change the source code (no sed).
For example I could do this by declaring tab in stack:
int tab[n][m][l];
but unfortunately this doesn't work if m
and n
are runtime variables.