C/C ++ array Initialization
The initialization of arrays in C/C ++ is incorrect because there are too many methods and chaotic rules, here we will make a simple summary of the issues that need to be careful about array initialization. If there are any omissions, we hope that the students will make some suggestions.
Static allocation of one-dimensional arrays
1.
Int a [3] = {0, 1, 2}; // correct int a [3] = {0, 1, 2, 3}; // error, the number of initialization values is greater than the array size. int a [3] = {0, 2}; // error. The initialization value is skipped. int a [3] = {0, 1 ,}; // error. The initialization value is skipped (even if it is the last element, adding a comma is considered to be skipped) int a [3] = {0}; // correct, the last element is ignored, and the last element is initialized to 0int a [n] = {0}; // note that n must be of the const type; otherwise, the error occurs.
2.
Char a [10] = "abceddddd"; // use a String constant to initialize the character array. Note: a [10] = '\ 0'
3.
Char a [10] = "abcd"; // when the character constant length is insufficient, other elements of the array are initialized as '\ 0'
4.
Int v1 [] = {1, 2, 3, 4}; char v2 [] = {'A', 'B', 'C', 0 }; // The size is not specified when the array is defined. when the list is initialized, the size of the array is determined by the number of list elements during initialization. So v1 and v2 are int [4] and char [4] types respectively.
Dynamic Allocation
1.
Int * a = new int [10]; // new allocates an uninitialized int array of 10 and returns a pointer to the first element of the array, this pointer initializes the pointer aa = {4,-3, 5,-2,-1, 2, 6,-2, 3}; // error, note that this array value assignment with braces can only be used for declaration. This is not Declaration, so an error occurs.
At this time, the value of a can only be passed through
1) int b[10]={...}; a = b;2) a[i]=2;3) memset(a, 1, sizeof(a));
2.
Int * a = new int [10] (); // each element is initialized to 0. Other values cannot be written in parentheses and can only be initialized to 0.
3.
Int * a = new int [n]; // note that n must be const
4. class initialization
Class Something {private: int myArray [10]; public: Something (): myArray {5, 5, 5, 5, 5, 5, 5, 5, 5} // initialization must be performed in the initialization list using braces. memset and other methods must be used for internal initialization in the constructor. {} Int ShowThingy (int what) {return myArray [what];}
Static allocation of two-dimensional arrays is slightly different from static allocation of one-dimensional arrays.
Int value [9] [9]; // The value of value [I] [j] Is uninitialized.
2.
Int value [9] [9] = {1, 1}, {2}; // value [0] [0, 1] and value [1] [0] value initialization, other initialization values are 0.
Dynamic Allocation 1.
Int (* value) [n] = new int [m] [n]; delete [] value; // n must be a constant, Which is intuitive to call. Not initialized
2.
Int ** value = new int * [m]; for (I) value [I] = new int [n]; for (I) delete [] value [I]; delete [] value; // The structure is analyzed multiple times, which is difficult to store and not initialized.