First, two-dimensional arrays are defined as follows:
A.
int Arr[2][3]={{1,2,3},{4,5,6}};
B.
int **Arr=new int* [2];for(int i=0;i<2;i++) Arr[i]=new int[3];//initializefor(int i=0;i<2;i++) for(int j=0;j<3;j++) Arr[i][j]=i*3+j+1;
However, there is an important difference between the two methods: the two-dimensional arrays declared by method A are stored continuously by row, while the rows declared by Method B are not stored continuously!
For example, both methods can beArr [I] [j]Or* (Arr + I) + j)Access element, but method A can access the element through the following access method similar to the one-dimensional array
* (Arr [0] + I * cols + j)
This access method is incorrect for method B.
Using the row-based continuous storage of A two-dimensional array, you can convert A two-dimensional array into A one-dimensional pointer as A function parameter, and it has good scalability:
1. If we know that there is A two-dimensional array Arr defined by method A with rows and cols columns, we can declare it as A function Func parameter as follows:
Void Func (int * A, int rows, int cols) {for (int I = 0; I
We may also want to use two-dimensional pointers as function parameters instead of one-dimensional pointers, as shown in figure
Void Func (int ** A, int rows, int cols );
In this caseFunc (Arr, 2, 3 );"Int (*) [3] type real participation in int ** type form parameters are not compatibleThat is, the parameter type does not match. Therefore, the two-dimensional array defined by method A can only be converted to one-dimensional pointer as the function parameter in the above way. Using two-dimensional pointers as function parameters this method applies to method B.
2. Because the two-dimensional array defined by Method B is not stored consecutively by row, the function parameter can only be a two-dimensional pointer, that is, the following statement
Void Func (int ** A, int rows, int cols) {for (int I = 0; I
In summary, to use A two-dimensional array as A function parameter, we must first specify that the two-dimensional array is defined in that way (A or B), and then adopt the corresponding function declaration method. The two function declaration methods have good scalability for different row and column numbers. The above is based on two-dimensional arrays, but the principles can be shared to three-dimensional and more multi-dimensional arrays, the processing method is the same.