Multidimensional arrays in c should be arrays of arrays, so a 3D array is an array whose elements are 2D arrays, and a 2D array is an array whose elements are 1D arrays.
int array[2][4][5];
int (*a)[4][5] = array; // 1)
int (*b)[5] = array[i]; // 2)
int (*c) = array[i][j]; // 3)
int d = array[i][j][k]; // 4)
First of all, it must be clear that the array name of a 1-dimensional array is the starting address of the 1-dimensional array.
The elements obtained from the multi-dimensional array, except for the lowest dimension, can be considered as the "array name", which is the starting address of the array. For example, if A[2] is taken from A4, then A[2] is the starting address of the second (counting from 0) 1-dimensional array in A4.
Then we start from 4). array[i][j][k] is just a value, so there is no problem.
Let’s look at 3) array[i][j] means that in this 3-dimensional array, I select the i-th 2-dimensional array, and then select the j-th 1-dimensional array in this 2-dimensional array, so array[i][j] is the starting address of this 1-dimensional array (can be understood as the array name of the 1-dimensional array), so use int (*c).
Skip 2) Let’s look at 1), array is the array name of the 3-dimensional array, so it is also the starting address of the 3-dimensional array, because its elements are 2-dimensional arrays, so we use int (*a)[4][5]
Then there are many * questions: For example, if this A[m][n] wants to access (i,j) , then use *(*(A + i) + j). Why, A is the name of a 2-dimensional array, so it is the starting point of a 2-dimensional array. The starting address, A + i is the address of the i-th element of A, take × to get its value, the value is a 1-dimensional array, which is the starting address of this 1-dimensional array, (A + i) + j gets the address of the jth element of this 1-dimensional array, and then takes to get this value.
Essentially, they are all arrays. Just like one-dimensional space is a straight line, two-dimensional space is many straight lines. In other words, two-dimensional space is obtained by extending one dimension, and so on for three-dimensional space.
Multidimensional arrays in c should be arrays of arrays, so a 3D array is an array whose elements are 2D arrays, and a 2D array is an array whose elements are 1D arrays.
First of all, it must be clear that the array name of a 1-dimensional array is the starting address of the 1-dimensional array.
The elements obtained from the multi-dimensional array, except for the lowest dimension, can be considered as the "array name", which is the starting address of the array. For example, if A[2] is taken from A4, then A[2] is the starting address of the second (counting from 0) 1-dimensional array in A4.
Then we start from 4).
array[i][j][k]
is just a value, so there is no problem.Let’s look at 3)
array[i][j]
means that in this 3-dimensional array, I select the i-th 2-dimensional array, and then select the j-th 1-dimensional array in this 2-dimensional array, soarray[i][j]
is the starting address of this 1-dimensional array (can be understood as the array name of the 1-dimensional array), so useint (*c)
.Skip 2) Let’s look at 1),
array
is the array name of the 3-dimensional array, so it is also the starting address of the 3-dimensional array, because its elements are 2-dimensional arrays, so we useint (*a)[4][5]
Then there are many * questions:
For example, if this
A[m][n]
wants to access(i,j)
, then use
*(*(A + i) + j)
. Why, A is the name of a 2-dimensional array, so it is the starting point of a 2-dimensional array. The starting address,A + i
is the address of the i-th element of A, take × to get its value, the value is a 1-dimensional array, which is the starting address of this 1-dimensional array, (A + i) + j gets the address of the jth element of this 1-dimensional array, and then takes to get this value.Essentially, they are all arrays. Just like one-dimensional space is a straight line, two-dimensional space is many straight lines. In other words, two-dimensional space is obtained by extending one dimension, and so on for three-dimensional space.