# Include <iostream>
Using STD: cout;
Using STD: Endl;
Int arr1 [5];
Int arr2 [5] = {1, 3, 5 };
Int main ()
{
Int arr3 [5];
Int arr4 [5] = {2, 4, 6 };
Int * PI1 = new int [5];
Int * Pi2 = new int [5] ();
For (INT I = 0; I! = 5; I ++)
Cout <arr1 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <arr2 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <arr3 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <arr4 [I] <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <* (PI1 + I) <"";
Cout <Endl;
For (INT I = 0; I! = 5; I ++)
Cout <* (Pi2 + I) <"";
Cout <Endl;
Return 0;
}
Results of Visual Studio 2005 compiling and running in Windows XP SP3:
Results of compiling and running eclipse + CDT + mingw in Windows XP SP3:
In Ubuntu Linux 8.04, eclipse + CDT + G ++ compiled and run:
The initialization results of the first four arrays in the above three results are the same. The following conclusions are obtained:
For arrays with built-in elements
Arr1: static allocation, defined in the function body, explicitly initialized without the initialization list, all elements are initialized to 0;
Arr2: static allocation, defined in the function in vitro, and initialized to 0 for elements that do not provide initial values in the initialization list;
Arr3: static allocation, defined in the function body, and explicitly initialized without the initialization list. elements are not initialized;
Arr4: static allocation, defined in the function body, initialization of elements that do not provide initial values in the initialization list is 0;
The initialization results of the last two arrays in the three results seem to depend on the compiler, and the results of mingw and G ++ are actually different (the results of mingw are too strange ). The initialization results of the first group (MS compiler) are consistent with those of the Dynamic Allocation array initialization rules described in C ++ primer (version 4), that is, when arrays are dynamically allocated, if the array element is of the built-in type, no Initialization is performed, for example, pi1. You can also use a pair of empty parentheses following the length of the array to initialize the value of the array element, such as pi2.