1. Assigning an initial value (designated initializers) assigning an initial value is a C99 addition, which allows us to initialize the specific elements of the array directly. C99 before, if we were to initialize an element in an array, such as a third element, we had to initialize the element before it. For example: int iarr[10] = {0, 0, 300}; And in C99, we can initialize a particular element like this: int iarr[10] = {[2] = 300}; / * Assign initialization iarr[2] to The remaining elements will be initialized to 0. Here we look at a small program. #include <stdio.h> int main (void) { int iarr[5] = {6, 3, [3] = 1, 5, [1] = 8};
printf ("%d\n", iarr[0]); printf ("%d\n", iarr[1]); printf ("%d\n", iarr[2]); printf ("%d\n", iarr[3]); printf ("%d\n", iarr[4]); return 0; } The output is: 6 8 0 1 5 You can see two points from this: A. If there is a value after the initial value is assigned, subsequent values are used to initialize the later elements. In the example above, IARR[3] is initialized to 1, and its subsequent element iarr[4] is initialized to 5. B. If the initialization of an element occurs more than once in the initialization list, the last time. In the example above, IARR[1] is initialized to 3 and then initialized to 8 by [1] = 8 assignment. 2. Assigning values to array elements We can use the subscript to assign a value to a particular element. For example: int iarr[5]; IARR[0] = 100; /* Assignment to the first element * * IARR[4] = 120; /* Assign value to Fifth Element/* IARR[2] = 180; /* Assign value to the third element * * C does not allow the use of arrays directly to assign values to other arrays, nor does it allow the use of an initialization list to assign values to a group. For example: int iarr_1[5] = {1, 2, 3, 4, 5}; /* Correct * * int iarr_2[5]; iarr_2 = iarr_1; /* ERROR! */ Iarr_2[5] = {3, 4, 5, 6, 7}; /* ERROR! */ IARR_2[5] = iarr_1[5]; /* Out of bounds! */ The last statement has crossed the line! Because both arrays have only 5 elements, the sixth element is accessed using subscript 5! 3. Array bounds (bounds) When using subscript, we must ensure that the subscript does not cross over. For example: int iarr[46]; The subscript range for this array is 0 to 45, and it is our responsibility to ensure that the subscript does not exceed this range because the compiler does not detect the subscript out of bounds! The C standard does not define the consequences of scaling out of bounds, that is, when a subscript crosses the line in the program we write, the program may work properly, or it may exit unexpectedly, and other strange situations may occur. #include <stdio.h> int main (void) { int var_1 = 20; int arr[5]; int var_2 = 40; printf ("var_1:%d, var_2:%d\n", var_1, var_2); ARR[-1] =-1; ARR[5] = 5; printf ("%d%d\n", arr[-1], arr[5]); printf ("var_1:%d, var_2:%d\n", var_1, var_2); return 0; } The output that the above program uses dev-c++ 4.9.9.2 to compile runs is: VAR_1:20, var_2:40 -1 5 VAR_1:20, var_2:-1 Visible, subscript crossing may change the value of other variables . This is because GCC (the C compiler used by dev-c++) saves the var_2 to the memory space before the array arr, so assigning a value to arr[-1 changes the var_2 value exactly. Different compiler compilation runs the program may have different output, or it may exit unexpectedly. The philosophy of C language is to trust programmers, and not to detect Cross-border programs running faster. When the program compiles, some of the underlying values are still unknown, so if you want to detect the subscript out of bounds, the compiler must add extra code to the generated target code to detect whether the subscript is out of bounds when the program runs, which can cause the program to run at a reduced rate. Therefore, in order to run efficiently, C does not detect whether the subscript is out of bounds. 4. Specify the number of array elements Before C99, when declaring an array, the value in [] must be an integer constant greater than 0 . In C99, when declaring an array, [] can be a variable . This is called a variable-length array (variable-length array, abbreviated VLA). When declaring a VLA, it cannot be initialized. In a subsequent tutorial, I will explain the VLA in detail. int n = 99; Double dbl_1[4]; /* Correct * * Double DBL_2[8/2 + 4]; /* Correct * * int iar_1[-5]; * * wrong! [] The value must be greater than 0 */ int iar_2[0]; * * wrong! [] The value must be greater than 0 */ int iar_3[9.2]; * * wrong! The value in [] must be an integer type */ Char Ch[n]; /* C99 not supported before! */ |