1, a two-dimensional array initialization
int arr[3][4] = {
{1,2,3,4},
{5,6,7,8},
{9,7,6,5}
}
How compiler allocates space:
int arr[3*4] = {1,2,3,4,5,6,7,8,9,7,6,5};
Disassembly:
Disassembly and a two-dimensional array array is no difference, is a piece of contiguous memory space;
The compiler needs a dimension obtained by multiplying the allocated memory space;
In short two-dimensional array just to achieve programmers look at the code more intuitive function;
2, the value of a given portion
If only part of the given value, other values 0s at compile time;
int arr[3][4] = {
{1,2},
{5},
{9}
}
int arr[3][4] = {
{1,2,3,4,5},
{5},
{9}
}
3, which is omitted {}
int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
int arr[3][4] = {1,2,3,4,5,6,7,8,9,10};
Because the difference between a two-dimensional array and the array is not essentially compile time, so there's omit the braces can be compiled by;
If only part of a given value, the remaining value will be supplemented by 0;
4, the length of the omitted
A length of the first two-dimensional array can be omitted, according to the second packet will automatically compile time length, if less than a 0 group;
For example: The following array will be divided into three groups, because the compiler until sets of four elements, from front to back will be automatically divided into three groups;
int arr[][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
Divided into three groups, a group of the last two complement 0:
int arr[][4] = {1,2,3,4,5,6,7,8,9,10};
Divided into two groups:
int arr[][4] = {1,2,3,4,5,6,7,8};
Note: this is wrong -> int arr [3] [];
If so, the compiler only until divided into three groups, each group and the number of elements can not be determined, not determined whether the complement of 0 or the like;
5. Summary
1, one-dimensional arrays and multidimensional arrays no difference in the disassembly.
2, if the number is not enough, other values will be zeros.
3, must not exceed the maximum length dimension.
4, may be omitted inside the {}
5, the value of the first dimension may be omitted
6. Find data in a multidimensional array
For example: two-digit group int [5] [12];
Looking compiler value arr [1] [7] is:
->arr[1*12+7]
If the array: int arr [n] [m] [k] [w] [r]
Looking: arr [2] [3] [4] [2] [2]
->arr[2*m*k*w*r+3*k*w*r+4*w*r+2*r+2]