Always think that int A [256] = {0}; Is to initialize all elements of a to 0, int A [256] = {1 }; is to initialize all elements of a to 1. when debugging, I found that the memory was not the same thing. I flipped through the C ++ programming language and finally made a conclusion. If the PDF file is not copied, it will be translated in this chapter. The following 5.2.1 array initialization array can be initialized with a column value, such as int V1 [] = {1, 2, 4 }; char V2 [] = {'A', 'B', 'C', 0}; the size is not specified when the array is defined. when the list is initialized, the array size is determined by the number of list elements during initialization. So V1 and V2 are int [4] and char [4] types respectively. If the array size is explicitly specified, an error occurs when the number of elements specified during initialization exceeds this size. For example: Char V3 [2] = {'A', 'B', 0}; // error: Too Many initialization values are char V3 [3] = {'A ', 'B', 0}; // if the number of elements specified during initialization is smaller than the array size, the remaining elements are initialized to 0. For example, int V5 [8] = {1, 2, 3, 4}; equivalent to int V5 [8] = {1, 2, 3, 4, 0, 0, 0}; note that no value is assigned to an array in the following format: void F () {V4 = {'C', 'D', 0}; // error: Not an array value assignment} if you want to copy the file like this, use Vector (chapter 16, section 3) or valarray (chapter 22, section 4 ). Character Arrays can be easily initialized using strings (see chapter 5, section 2.2). Note: Char Alpha [] = "abcdefghijklmn"; the C ++ programming language, third edition by Bjarne stroustrup. ,,,,,,,,,,,,,,,,,,,,,,,,,,,///////////// //////////////////////////////////////// //////////// 6.6 set initialization as the name suggests, aggregate is a collection of multiple things. This definition includes a set of mixed types, such as struct and Class. arrays are a collection of a single type. The initialization set is often lengthy and error-prone, while the C ++ aggregate initialization becomes convenient and secure. When a collection object is generated, you only need to specify the initial value, and then the initialization is undertaken by the compiler. This can be specified in several different styles, depending on the type of the collection being processed. In either case, the specified initial values must be enclosed in braces. For example, an internal array can be defined as: int A [5] = {1, 2, 3, 4, 5}; if the given initialization value is greater than the number of array elements, the compiler will provide an error message. But what if Initialization is less than the number of data elements? For example, int B [6] = {0}. In this case, the compiler assigns the first initialization value to the first element of the array, and then assigns 0 to the remaining elements. NOTE: If an array is defined without an initial value, the compiler does not initialize the array. Therefore, the above expression is a concise method to initialize an array to zero. It does not need to use a for loop, and avoids the "offset 1 bit" error (it may be more effective than the for loop, this depends on the compiler ). An array also has a fast initialization method called automatic count, that is, let the compiler determine the size of the array based on the number of initialization values: int C [] = {1, 2, 3, 4}; now, if you want to add another element to this array, you only need to add an initialization value. If you want to create our code, you only need to modify it in one place. In this way, the chances of errors during modification are reduced. But how can we determine the size of this array? Use the expression sizeof C/sizeof * C (the size of the entire array divided by the size of the first element). In this way, it does not need to be modified when the array size changes. For (INT I = 0; I <sizeof C/sizeof * C; I ++) C [I] ++; because the structure is also a collection type, so they can also be initialized in the same way. Because all the members of the C-style struct are public, their values can be specified directly. Struct X {int I; float F; char C ;}; x X1 = {1, 2.2, 'C'}; if there is an array of such struct, you can also use nested braces to initialize each object: X X2 [3] ={{ 1, 1.1, 'A'}, {2, 2.2, 'B '}}; here, the third object is initialized to zero. If struct has a private member (typically a well-designed class in C ++), or even if all the members are public members but there is a constructor, the situation is different. In the preceding example, the initial value is directly assigned to each element in the set, but the constructor enforces initialization through a formal interface. Here, the constructor must be called to complete initialization. Therefore, if there is a following struct type: struct y {float F; int I; y (int );}; the constructor call must be indicated. The best method is as follows: Y Y1 [] = {Y (1), y (2), y (3 )}; in this way, three objects and three constructor calls are obtained. As long as there is a constructor, whether all members are public struct or a class with private members, all initialization work must be done through the constructor, even if a collection is being initialized. The following is another example of multi-constructor parameters: //: c06: multiarg. CPP // from thinking in C ++, 2nd Edition // available at http://www.BruceEckel.com // (c) bruce Eckel 2000 // copyright notice in copyright.txt // multiple constructor arguments // with aggregate initialization # include <iostream> using namespace STD; Class Z {int I, j; public: Z (int ii, int JJ); void print () ;}; Z: Z (int ii, int JJ) {I = II; j = JJ ;} void Z:: Print () {cout <<"I =" <I <", j =" <j <Endl;} int main () {z ZZ [] = {z ), Z (3, 4), Z (5, 6), Z (7, 8)}; For (INT I = 0; I <sizeof ZZ/sizeof * ZZ; I ++) ZZ [I]. print ();}///:~ Note: This looks like an explicit constructor is called for every object in the array. ,,,,,,,,,,,,//////////////////////////// //// // This issue involves data type initialization, the data type initialization in C language is summarized as follows: simple variable initialization form: data type variable name = initialization value; for example, defining integer variable, the statement with the initialization value 10 is as follows: int A = 10; the initialization of the array requires initialization of its array elements through a constant data list, in the form of the following: data Type array name [array length] = {initialization value 1, initialization value 2 ,..., Initialization value n}. For example, define an integer array with a length of 5 and initialize the array with the following statements: int A [5] = {20, 21, 4 }; the initialization method of struct variables is similar to that of arrays. The Initialization Method of struct member variables is simple variables or arrays. The specific form is as follows: struct identifier {member variable list ;... }; Struct identifier variable name = {initialization value 1, initialization value 2 ,..., Initialization value n}. For example, define a variable of the struct point type and initialize the statement as follows: struct point OP1 = {0.0, 0.2, 0.3 }; the values of the three member variables of the struct point type variable OP1 are op1.x = 0.0 and op1.y = 0.2 op1.z = 0.3. There are three methods to define struct variables, therefore, there are three methods for initializing struct variables. One of the methods is described above. The other two methods are as follows: struct point {Double X; Double Y; Double Z ;} OP1 = {0.0, 0.2, 0.3}; struct {Double X; Double Y; Double Z;} OP1 = {0.0, 0.2, 0.3}; When initializing a struct variable, you can either Initialize all of its member variables or initialize only some of its member variables. For example, struct student {long ID; char name [20]; char sex;} A = {0}; it is equivalent to. id = 0;. name = "";. sex = '\ 0x0 '. Only initialize some of the member variables. At least one of the initialized data is required. Other uninitialized member variables are initialized by the system to provide default initialization values. The default value of initialization for member variables of various basic data types is shown in Table 9-1. Default data type initialization value: int 0 char '\ 0x0' float 0.0 double 0.0 char array [N] "int array [N ..., 0} initialization of variable of complex struct type also follows the above rules and assigns initialization values to the struct member variables respectively. Example: struct line {int ID; struct point startpoint; struct point endpoint;} oline1 = {0, {0, 0}, {, 0 }}; here, constant 0 is used to initialize the ID of the basic type member variable of oline1; Constant list {0, 0} is used to initialize the struct point type member variable of oline1 startpoint; Constant list {, 0, 0} is used to initialize the endpoint of the struct point type member variable of oline1.