Hi, I'm writing C code for a project and I would like to know (if it's possible) how to make an array of arrays. I mean I want to place a bidimensional array inside each one of the elements of an unidimensional array.
Thank you in advance.
Hi, I'm writing C code for a project and I would like to know (if it's possible) how to make an array of arrays. I mean I want to place a bidimensional array inside each one of the elements of an unidimensional array.
Thank you in advance.
1D integer array: int array1d[10];
access via: array1d[index];
2D integer array: int array2d[10][10];
access via: array2d[indexY][indexX];
3D integer array: int array3d[10][10][10];
access via: array3d[inxexZ][indexY][indexX];
Etc.
It is a bit more complicated if you want a fairly big array, which isn't uncommon for 3D arrays. In that case you will need to dynamically allocate memory, because statically allocated memory can overflow the stack. An example is Z, X, Y being > 1000.
Do that like this for a Z*Y*X 3d-array:
and access normally with array[z][y][x];Code:int ***array; for (int z = 0; z < Z; ++z) { array[z] = malloc(Y * sizeof(*int)); for (int y = 0; y < Y; ++y) array[z][y] = malloc (X * sizeof(int)); }
(though I believe there is a bit better solution, which kind of bored to think atm)