Everyone is making this too hard. There are no 3D arrays in C or C++. All there are are one dimensional arrays. The first index specifies the number of elements:
-
int arr[3]; //has 3 elements. Each element an int
-
int arr[3][7]; //has 3 elements. Each element an array of 7 int
-
int arr[3][4][5]; //has 3 elements. Each element an array 4 arrays of 5 ints.
-
Further, arrays must be contiguous in memory. Element 0 then element 1, etc.
So you allocate int arr[3][4[5] as:
-
int* arr = malloc(3 * 4 * 5 * sizeof(int));
-
You now have 60 ints. If you want to use this as a 3D array then tou need to cast the return from malloc to the correct pointer. In this case, a pointer to a 4x5 array of int:
-
int (* arr)[4][5] =(int (*)[4][5] )malloc(3 * 4 * 5 * sizeof(int));
-
Remember, the name of an array is always the address of element 0. In this case the elements are 4x5 arrays. Using an int*** is a pointer to a pointer to a pointer to a
single int. Trying to use this as a 3D array will produce the wrong address calculations.