Zookeeper

I. Two-dimensional array Initialization

The two-dimensional array initialization form is:

Data Type array name [whole constant expression] [whole constant expression] = {initialization data };

The initial values of each array element are given in {}, separated by commas. Assign the initial values in {} to each array element in sequence.

There are several initialization methods:

(1) initialize the branch

Int A [2] [3] = {1, 2, 3}, {4, 5, 6 }};

In {}, separate each row with {}. The initial values 1, 2, and 3 in the first pair of {} are the initial values of the three elements in row 0. The initial values and 6 in the second {} are the initial values of the three elements in the first row. Execute the following statement:

Int A [2] [3];

A [0] [0] = 1; A [0] [1] = 2; A [0] [2] = 3; A [1] [0] = 4; A [1] [1] = 5; A [1] [2] = 6;

Note: The number of initialized data cannot exceed the number of array elements. Otherwise, an error occurs.

(2) Non-branch Initialization

Int A [2] [3] = {1, 2, 3, 4, 5, 6 };

Assign the data in {} to each element of array a in sequence (assign values by row ). That is, a [0] [0] = 1; A [0] [1] = 2; A [0] [2] = 3; A [1] [0] = 4; A [1] [1] = 5; A [1] [2] = 6;

(3) initialize some array elements

Static int A [2] [3] = {1}, {4 }};

The first row has only two initial values, which are assigned to a [0] [0] And a [0] [1] in order. the initial value 4 of the second row is assigned to a [1] [0]. Because the storage type is static, the initial values of other array elements are 0. Note: In some C language systems (such as Turbo C), the initial values of variables or arrays whose storage type is not static are also 0.

Static int A [2] [3] = {1, 2 };

There are only two initial values, that is, a [0] [0] = 1, a [0] [1] = 2, and the initial values of other array elements are 0.

(4) The definition of the first dimension can be omitted, but the definition of the second dimension cannot be omitted. The system determines the length of the first dimension based on the number of initialized data and the length of 2nd dimensions.

Int A [] [3] = {1, 2, 3, 4, 5, 6 };

The definition of the first dimension of array a is omitted. There are 6 initial data records. The length of the second dimension is 3, that is, the number of three rows. Therefore, the first dimension of array a is 2.

Generally, when the definition of the first dimension is omitted, the size of the first dimension is determined according to the following rules:

The number of initial values can be divisible by the second dimension, and the quotient obtained is the size of the first dimension. If the number cannot be divisible, the size of the first dimension is the quotient plus 1. For example, int A [] [3] = {1, 2, 3, 4}; equivalent to: int A [2] [3] = {1, 2, 3 };

If the branch is initialized, the definition of the first dimension can also be omitted. The following array definitions have two pairs {}, indicating that array A has two rows.

Static int A [] [3] = {1, 2}, {4 }};

2. Two-dimensional array and pointer

If the addresses of two-dimensional arrays and pointers 1 and two-dimensional arrays and array elements are defined as follows: int * P, A [3] [4];

1) two-dimensional array a consists of several one-dimensional arrays. The two-dimensional array defined in C language is actually a one-dimensional array, and each member of this one-dimensional array is a one-dimensional array. For example, the preceding a array is composed of three elements: a [0], a [1], and a [2, each element, such as a [0], a [1], and a [2], is a one-dimensional array composed of four integer elements. Use a [0] [0] And a [0] [1] to reference each element in a [0], and so on. As explained in section 2, in C, the name of a one-dimensional array defined in the function body or outside the function is an address constant and its value is the address of the first element of the array, the base type of this address is the type of array elements. In the preceding two-dimensional array, a [0], a [1], and a [2] are all one-dimensional array names, which also represent an immutable address variable, the values are the addresses of the first element in each line of the Two-dimensional array. The base type is the type of the array element. Therefore, for two-dimensional arrays, expressions such as a [0] ++ are invalid. If expression a [0] + 1 is present, the unit of 1 in the expression should be 4 bytes. In the preceding definition, the base type of the pointer Variable P is the same as that of a [I] (0 ≤ I <3). Therefore, the value assignment statement P = A [I]; is valid. We know that a [I] can also be written as: * (a + I), so the above value assignment statement can also be written as: P = * (a + I );.

2) The two-dimensional array name is also an address constant, and the two-dimensional array name is also a pointer to store the address constant. Its value is the address of the first element in the two-dimensional array. In the preceding array A, the value of array name a is the same as the value of a [0], but its base type is the array type with four integer elements. That is, the value of A + 0 is the same as that of a [0]. The value of A + 1 is the same as that of a [1, the values of A + 2 are the same as those of a [2], indicating the first address of the first, first, and second rows in array. The two-dimensional array name should be considered as a row pointer. In expression A + 1, the unit of value 1 should be 4 × 4 bytes, rather than 4 bytes. The value assignment statement P = A; is invalid because the base types of P and a are different. Similarly, for two-dimensional array name a, a ++, A = a + I, and other operations are not allowed.

3) address of a two-dimensional array element. The address of a two-dimensional array element can be obtained by Expression & A [I] [J]. It can also be expressed by the first address of each line. In the preceding two-dimensional array A, the addresses of each element can be expressed by the first addresses of each line, such as a [0], a [1], and a [2. For example, the address & A [0] [0] can be expressed by a [0] + 0, address & A [0] [1] can be expressed as a [0] + 1; if 0 ≤ I <3, 0 ≤ j <4, then the address of a [I] [J] can be obtained using the following five expressions: (1) & A [I] [J] (2) A [I] + J (3) * (a + I) + J (4) & A [0] [0] + 4 * I + J (5) A [0] + 4 * I + J

In the preceding expressions, the base types of a [I], & A [0] [0], and a [0] are all int type, the system automatically determines that the unit of constant 1 in the expression is 4 bytes. However, the expression for finding the address of a [I] [J] cannot be written as a + 4 * I + J, because the base type of A is the array type of four integer elements, the system automatically determines that the unit of constant 1 is 16 bytes.

2. if the address is used to reference a two-dimensional array element, the following definitions are provided: int A [3] [4], I, J; when 0 ≤ I <3, 0 ≤ j <4, array A can be referenced using the following five expressions: (1) a [I] [J] (2) * (a [I] + J) (3) * (a + I) + J) (4) (* (a + I) [J] (5) * (& A [0] [0] + 4 * I + J)

In (2), because the base type of expression * (a [I] + J) is int, the displacement of J is 4 × J bytes.

In (3), in the expression * (a + I) + J), the base type of A is an array of four elements, the displacement of I is 4 × 4 × I bytes, while the base type of * (a + I) is int, and the displacement of J is still 4 × J bytes.

In (4), a pair of parentheses outside * (a + I) is indispensable. If it is written as * (a + I) [J], because the operator [] has a higher priority than the * sign, the expression can be converted to: * (a + I) + J), that is: * (a + I + J), then I + J will make the displacement 4 × 4 × (I + J) bytes, this is not the address of element a [I] [J. * (A + I + J) is equivalent to * (a [I + J]) and a [I + J] [0]. the referenced array element a [I + J] [0], rather than a [I] [J], may already be out of the range defined by the array.

In (5), & A [0] [0] + 4 * I + j Represents the address of array element a [I] [J, through the interaddress operator *, the expression * (& A [0] [0] + 4 * I + J) represents the storage unit of the array element a [I] [J.

3. Create a pointer array to reference two-dimensional array elements. If the following definitions are available: int * P [3], a [3] [2], I, j; here, note * P [3] also follows the operator's priority. A pair of [] has a higher priority than *. Therefore, P is first combined with [] to Form P [3], it indicates that P is an array name, and the system will open up three consecutive storage units for it. The * sign before it indicates that array P is a pointer type, each of its elements is a pointer with the base type of Int. If the condition is 0 ≤ I <3, the base types of P [I] And a [I] are the same, and P [I] = A [I] is a legal value assignment expression.

For (I = 0; I <3; I ++) P [I] = A [I]; here, the A [I] on the right of the assignment number is a constant, indicating the first address of each row in array A. The P [I] on the left of the assignment number is a pointer variable, the result of loop execution Points P [0], p [1], and P [2] to the beginning of each row in array.

When the element in P points to the beginning of each row in array A, the Reference Form of element a [I] [J] in array a * (a [I] + J) it is equivalent to * (p [I] + J. It can be seen that the elements of array A can be referenced through the pointer array P. Their equivalent forms are as follows: (1) * (p [I] + J) (2) * (p + I) + J) (3) (* (p + I) [J] (4) P [I] [J] different: the values in P [I] are variable, while those in a [I] are immutable.

If a two-dimensional array element is referenced by creating a row pointer: int A [3] [2], (* PRT) [2]; here, note (* PRT) [2], because of the existence of a pair of parentheses, the * sign is first combined with the PRT, indicating that the PRT is a pointer variable, then it is combined with the description [2], indicating that the base type of the pointer PRT is an array containing two int elements. Here, the base type of PRT is the same as that of a, so PRT = A; is a valid value assignment statement. PRT + 1 is equivalent to a + 1 and equivalent to a [1]. When PRT points to the beginning of array A, you can reference A [I] [J] in the following form: (1) * (PRT [I] + J) (2) * (PRT + I) + J) (3) (* (PRT + I) [J] (4) PRT [I] [J] Here, PRT is a pointer variable with variable values, while a is a constant.

For arrays using char Arrays:

Const char * cities [5]; // array of pointers to 5 strings

Const char [25] [5]; // array of 5 arrays of 25 char

Two-dimensional array and pointer