One, about two-dimensional arrays and two-dimensional array differences
(1) One-dimensional arrays are stored continuously in memory, and similarly, two-dimensional arrays are stored continuously in memory. So from the memory point of view, one-dimensional arrays and two-dimensional arrays are actually not fundamentally different.
(2) Two-dimensional arrays can be replaced with one-dimensional arrays. However, in practical applications, it is sometimes more intuitive to use two-dimensional arrays to facilitate program programming.
(3) The two are identical in memory usage efficiency.
The first and second dimensional concepts of two or two-D arrays
(1) For example, int a[2][5], the preceding 2 represents the first dimension; the latter 5 indicates the second dimension
(2) The first dimension of a two-dimensional array represents the outer layer, and the first dimension itself is an array of 2 elements, and the two elements are arrays of the second dimension respectively. The second-dimensional array itself is an array of elements that are normal int variables.
Subscript access to three or two-D arrays
Example 1:
1 inta[2][5] = {{1,2,3,4,5},{6,7,8,9,Ten}};2 int(*p) [5];//defines an array pointer3p =A;4 5printf"a[1][2] =%d.\n", a[1][2]);//a[1][2] = 86printf"(* (p+1) +1) =%d.\n", * (* (p+1)+2));//A[1][2]
Operation Result:
Iv. several symbols to be understood about two-dimensional data
Example 2: Understanding A, &a, a[0], &a[0], a[0][0], &a[0][0]
1 /*2 testing of several symbols for two-dimensional arrays3 1, A is equivalent to &a[0]4 2, a[0] equivalent to &a[0][0]5 3, the value of a, &a, a[0], &a[0], &a[0][0] is equal, but there is a difference between the type. 6 */7 inta[2][5] = {{1,2,3,4,5},{6,7,8,9,Ten}};8 9printf"A =%p.\n", a);//a type is int (*) [5]Tenprintf"&a =%p.\n", &a);//&a type is int (*) [2][5] Oneprintf"a[0] =%p.\n", a[0]);//a[0] Type is int * Aprintf"&a[0] =%p.\n", &a[0]);//&a[0] Type is int (*) [5] -printf"a[0][0] =%d.\n", a[0][0]);//a[0][0] type is int -printf"&a[0][0] =%p.\n", &a[0][0]);//&a[0][0] Type is int *
Operation Result:
Example 3: Use of array pointers in the first and second dimensions
1 //the use of two-dimensional arrays in combination with pointers2 inta[2][5] = {{1,2,3,4,5},{6,7,8,9,Ten}};3 4 int(*P1) [5];//Array Pointers5 int*P2;//General Pointers6P1 = A;//equivalent P1 = &a[0]; //array name pointing to a two-dimensional array7P2 = a[0];//equivalent P2 = &a[0][0]; //the first-dimensional array pointing to a two-dimensional array8 9printf"a[0][2] =%d.\n", * (* (p1+0)+2));//a[0][2] = 3Tenprintf"a[1][2] =%d.\n", * (* (p1+1)+2));//a[1][2] = 8 One Aprintf"a[0][2] =%d.\n", * (p2+2));//a[0][2] = 3 -printf"a[0][4] =%d.\n", * (p2+4));//A[0][4] = 5
Operation Result:
V. Summary
(1) need to understand the nature of the two-dimensional array and the meaning of several symbols.
(2) Two-dimensional arrays and arrays of pointers are closely related. Learning to manipulate two-dimensional arrays using array pointers will deepen understanding when used in practice.
Note: The Learning notes section is extracted from the Zhu Youpeng Teacher's IoT video tutorial, which is hereby declared.
C Language Notes (two-dimensional arrays and numeric pointers)