[Study Notes] [C Language] Two-dimensional array, two-dimensional array of Study Notes
1. What is a two-dimensional array?
An array can represent the age of a class. What if I want to represent many classes?
What is a two-dimensional array? Int ages [3] [10]; three classes, each with 10 employees
Equivalent to 3 rows and 10 columns
It is equivalent to three one-dimensional arrays.
A two-dimensional array is a special one-dimensional array. Its elements are one-dimensional arrays. For example, int a [2] [3] can be considered to be composed of one-dimensional array a [0] and one-dimensional array a [1]. Both one-dimensional arrays contain three int-type elements.
2. Two-dimensional array Definition
A two-dimensional array is defined in the following form:
Type specifier array name [constant expression 1] [constant expression 2]
Here, constant expression 1 indicates the length of the First-dimensional downlink object, and constant expression 2 indicates the length of the second-dimensional downlink object. For example:
Int a [3] [4];
It indicates an array of three rows and four columns. The array name is a, and its subscript variable type is integer. There are 3*4 subscript variables in the array, namely:
A [0] [0], a [0] [1], a [0] [2], a [0] [3]
A [1] [0], a [1] [1], a [1] [2], a [1] [3]
A [2] [0], a [2] [1], a [2] [2], a [2] [3]
A two-dimensional array is conceptually two-dimensional, that is, its subscript changes in two directions, and its position in the array is also in a plane, rather than just a vector like a one-dimensional array. However, the actual hardware memory is sequential, that is, the memory units are arranged linearly in one dimension. There are two ways to store two-dimensional arrays in One-Dimensional Memory: one is to arrange by row, that is, to put a row into the second row. The other is to sort by column, that is, put a column and then put it into the second column. In C, two-dimensional arrays are arranged in rows. That is, first store Row a [0], then Row a [1], and finally Row a [2. Four elements in each row are also stored in sequence. Array a indicates the int type, which occupies two bytes of memory, so each element occupies two bytes.
3. Reference of two-dimensional array elements
Elements of a two-dimensional array, also known as double subscript variables, are expressed:
Array name [subscript] [subscript]
The subscript must be an integer constant or an integer expression. For example:
A [3] [4]
It indicates the elements of array a with three rows and four columns.
Subscript variables are similar to arrays, but they have different meanings. The square brackets in the array description indicate the length of a certain dimension, and the maximum value of the mark can be removed;
The subscript in the array element indicates the position of the element in the array. The former can only be a constant, and the latter can be a constant, variable, or expression.
4. Code
1 int main () 2 {3/* 4 int ages [5] = {10, 11, 90, 89, 70 }; 5 6 int ages2 [3] [5] = {7 {10, 11, 90, 89, 70}, 8 9 {10, 11, 90, 89, 70 }, 10 11 {10, 11, 90, 89, 70} 12}; */13 char cs [2] [3]; 14 15/* 16 1 light blue 17 2 dark blue 18 3 yellow 19 4 Red 20-1 No 21 */22 23 int cubes [5] [5] = {24 {1, -1,-1,-1,-1}, 25 {1, 1,-1, 2,-1}, 26 {4, 1, 2, 2, 2}, 27 {4, 3, 3, 3}, 28 {4, 3, 3, 3, 3} 29 }; 30 31/* 32 1 white 33 2 black 34-1 No 35 */36 int wuzi [6] [6] = {37 {}; 38 }; 39 40 41 return 0; 42}