C + + array initialization because there are too many methods, and the rules are also confusing, so in the use of the array initialization is often the wrong question, here to the initialization of the array needs to be cautious about the problem of a simple summary, there are missing the hope that the students together to point out the supplementary.
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 initialized values is greater than the array size int a[3]={0,,2}; Error, initialization value skipped int a[3]={0,1,}; Error, initialization value is skipped (even if the last element, adding a comma is also considered to be skipped) int a[3]={0}; Correct, omit initialization of the last element, and the last omitted element is initialized to 0int a[n]={0}; Note N must be a const type, otherwise the error
2.
Char a[10] = "ABCEDDDDD"; Initializes an array of characters with a string constant. Note: a[10]= '
3.
Char a[10] = "ABCD"; When a character constant is not long enough, the other elements of the array are initialized to '
4.
int v1[] ={1,2,3,4};char v2[]={' A ', ' B ', ' C ', 0}; When the array is defined with no size specified, the size of the array is determined by the number of list elements initialized when initialization takes the list 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 size 10 and returns a pointer to the first element of the array, which initializes the pointer aa = {4,-3, 5,-2,-1, 2, 6, 2, 3}; Error, note that this array assignment with curly braces can only be used when declaring, there is no declaration here, so there is an error.
Then the assignment to a can only be done by
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, and no other value can be written in parentheses, only initialized to 0
3.
int* A = new int[n];//note N must be const
4. initialization in a class
Class Something {private:int myarray[10];p ublic:something (): MyArray {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}//Using curly braces initialization must be entered in the initialization list A method such as memset is initialized within the constructor to initialize it. {}int showthingy (int what) { return myarray[what];}}
Static assignment of two-dimensional array static allocation and one-dimensional difference of less than 1.
int value[9][9]; VALUE[I][J] has a variable value, not initialized
2.
int value[9][9] = {{1,1},{2}},//value[0][0,1] and value[1][0] values initialized, others initialized to 0
Dynamic allocation 1.
int (*value) [n] = new Int[m][n];d elete []value; n must be a constant, call intuitive. Not initialized
2.
int** value = new int* [M];for (i) value[i] = new Int[n];for (i) delete []value[i];d elete []value; Multiple destruction, storage trouble, uninitialized
Initialization of A/C + + array