c++ - C语言多维数组问题,本人小白刚学c
PHP中文网
PHP中文网 2017-04-17 15:28:07
0
2
585

请问各位大神,二维数组和三维数组的本质是神马?为啥要取多次*呢?那多维数组呢?对一维、二维、三维等数组名取地址取得是神马呢?请说得仔细点,本人小白,谢谢各位大神了

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
伊谢尔伦

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)
  1. 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.

  2. 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.

  3. Then we start from 4). array[i][j][k] is just a value, so there is no problem.

  4. 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).

  5. 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.

Peter_Zhu

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!