Page 50 - C-Language
P. 50
#define ARRLEN 10000
int array[ARRLEN][ARRLEN];
size_t i, j;
for (i = 0; i < ARRLEN; ++i)
{
for(j = 0; j < ARRLEN; ++j)
{
array[i][j] = 0;
}
}
In the same vein, this is why when dealing with an array with one dimension and multiple indexes
(let's say 2 dimensions here for simplicity with indexes i and j) it is important to iterate through the
array like this:
#define DIM_X 10
#define DIM_Y 20
int array[DIM_X*DIM_Y];
size_t i, j;
for (i = 0; i < DIM_X; ++i)
{
for(j = 0; j < DIM_Y; ++j)
{
array[i*DIM_Y+j] = 0;
}
}
Or with 3 dimensions and indexes i,j and k:
#define DIM_X 10
#define DIM_Y 20
#define DIM_Z 30
int array[DIM_X*DIM_Y*DIM_Z];
size_t i, j, k;
for (i = 0; i < DIM_X; ++i)
{
for(j = 0; j < DIM_Y; ++j)
{
for (k = 0; k < DIM_Z; ++k)
{
array[i*DIM_Y*DIM_Z+j*DIM_Z+k] = 0;
}
}
}
Or in a more generic way, when we have an array with N1 x N2 x ... x Nd elements, d dimensions
and indices noted as n1,n2,...,nd the offset is calculated like this
https://riptutorial.com/ 26

