There are many easy-to-forget points for two-dimensional arrays, along with many pitfalls, which the compiler complains about.
Free to do nothing, for the future forget, then organize this note.
Method 1: Pass a two-dimensional array with the number of columns.
1 Const introw =2; 2 Const intCol =3;3 4 intMatrix[][col] = {{0,1,2},{3,4,5} };5 6 voidDisplayintMatrix[][col])7 {8 for(inti =0; I! = row; i++ )9 {Ten for(intj =0; J! = col; J + + ) One { Acout<<matrix[i][j]<<" "; - } -cout<<Endl; the } -}
Method 2: Passing the number of columns of one-dimensional pointers and arrays
1 voidDisplayint* Matrix,intCol)2 {3 for(inti =0; I! = row; i++ )4 {5 for(intj =0; J! = col; J + + )6 {7cout<<* (MATRIX+I*COL+J) <<" ";8 }9cout<<Endl;Ten } One } A - intMain () - { theDisplay (matrix[0],COL);//display (&matrix[0][0],col); - return 0; -}
Method 3: Passing pointers to arrays
1 voidDisplayint(*matrix) [col])2 {3 for(inti =0; I! = row; i++ )4 {5 for(intj =0; J! = col; J + + )6 {7cout<<* (* (matrix+i) +j) <<" ";8 }9cout<<Endl;Ten } One } A - intMain () - { theDisplay (matrix);//two-dimensional array name passing the first address of a two-dimensional array (pointer to the array length col int (*) [3]) - return 0; -}
In fact, these three ways are not very different.
Can be understood as a one-dimensional pointer to a fixed-length array.
e.g. int *matrix, [0,0] [0,1] [0,2] ... When the number of rows is 2 o'clock, the solution gets the contents of the address in turn. In fact, the memory that the array opens up is continuously arranged. This explains the way in which the cout<<* (matrix+i*col+j) << " ;" In Law 2 was introduced.
Basic knowledge should not be difficult to grasp.
Now let's do some interesting things ~
Expansion 1:
1typedefintMatrixtype[row][col];//define a matrix type of 2 * 32 3Matrixtype Mymatrix = {{0,1,2},{3,4,5} };4 5 voidDisplay (matrixtype* Mymatrix)//pointer to matrix type6 {7 for(inti =0; I! = row; i++ )8 {9 for(intj =0; J! = col; J + + )Ten { Onecout<<* (* (matrix+i) +j) <<" "; A } -cout<<Endl; - } the } - - intMain () - { +Display (&Mymatrix); - return 0; +}
Then look at another way to define a two-dimensional array.
Expansion 2:
1typedefintColtype[col];//Coltype is a one-dimensional array (pointer) type with an array length of Col2typedef coltype MATRIXTYPE[ROW];//Matrixtype is a two-dimensional array type of row * Col3 4Matrixtype Mymatrix = {{0,1,2},{3,4,5} };5 6 voidDisplay (coltype* Mymatrix)//equivalent to int (*matrix) [col]7 {8 for(inti =0; I! = row; i++ )9 {Ten for(intj =0; J! = col; J + + ) One { Acout<<* (* (matrix+i) +j) <<" "; - } -cout<<Endl; the } - } - - intMain () + { -coltype* matrix_ptr =Mymatrix; +Display (MATRIX_PTR);//display (Mymatrix); A return 0; at}
All of these are static two-dimensional array delivery methods.
Add a method that dynamically opens up a two-dimensional array.
Expansion 3:
1 intMain ()2 {3 int**arr_2d;4 introw =2;//available for CIN5 intCol =3;6 7arr_2d = (int**)malloc(row*sizeof(int*));//arr_2d = new int* (row);8 9 for(inti =0; I! = row; i++ )Ten{//Open Line Memory One* (arr_2d+i) = (int*)malloc(col*sizeof(int)); A } - - for(inti =0; I! = row; i++ ) the { - for(intj =0; J! = col; J + + ) - { -Cin>>Arr_2d[i][j]; + } - } + A for(inti =0; I! = row; i++ ) at { - for(intj =0; J! = col; J + + ) - { -cout<<arr_2d[i][j]<<" "; - } - } in - for(inti = row-1; I >=0; i-- ) to { + Free(Arr_2d[i]); - } the Free(arr_2d); * return 0; $}
The opening of a two-dimensional array is to first open up the line of memory, and then open the column memory. When released in reverse order, the column memory is freed before the row memory is freed.
Specific reference to the resolution of the Great God ~ http://blog.csdn.net/morewindows/article/details/7664479
Convert Dimension Array