How to pass a two-dimensional array as a function parameter is an issue that is frequently encountered when dealing with multidimensional arrays. For a long time, we often know it, but do not know why. Here is a brief summary.
1. "C Programming" refers to: you can use a two-dimensional array is an argument or a parameter, in the called function to define the shape parameter group can specify the size of all dimensions, you can omit the size of the first dimension description, such as:
void func (int array[3][10]); void func (int array[][10]);
Both are legal and equivalent, but cannot omit the size of the second or higher dimension. The two sample programs are as follows:
#include <iostream>using namespace Std;void fun (int. a[2][2],int N) {for (int. i=0;i<n;i++) {for ( int j=0;j<n;j++) { cout<<a[i][j]<<endl; }} } void Main () { int test[2][2]={{1,2},{3,4}}; Fun (test, 2); Cin.get ();}
The above program provides the size of the two dimensions in a two-dimensional array, and then look at the following:
#include <iostream>using namespace Std;void fun (int. a[][2],int N) {for (int. i=0;i<n;i++) {for ( int j=0;j<n;j++) { cout<<a[i][j]<<endl; }} } void Main () { int test[2][2]={{1,2},{3,4}}; Fun (test, 2); Cin.get ();}
The above two procedures can be run normally, the output result:
Why do we have to provide the size of the second dimension? Because of the compile phase, the compiler should address a[i][j] correctly. The address of A[i][j] is: a+i* Number of columns (second dimension size) +j.
2. If the two-dimensional pointer int * * is passed in the function parameter, the two-dimensional pointer contains a, A[0]/A[1], data three-layer structure.
int** has provided the address of a[0] and a[1], so that the second dimension is not required to determine the number of a+i* columns (the second dimension) in the process of addressing a[i][j]. The procedure is as follows:
#include <iostream>using namespace Std;void fun (int. **a,int N) {for (int. i=0;i<n;i++) {for (int j= 0;j<n;j++) { cout<<a[i][j]<<endl; } }} void Main () { int **test = new Int*[2]; for (int i=0;i<2;i++) { Test[i] = new int[2]; } Test[0][0] = 1;test[0][1] = 2;test[1][0] = 3;test[1][1] = 4; Fun (test, 2); Cin.get ();}
3. If you pass a two-dimensional array name to int**, the compile phase will give an error: You cannot convert parameter 1 from "int [2][2]" to "int * * *." Because the structure of two-dimensional arrays and the two-dimensional pointers are different, at the bottom, the structure of two-dimensional arrays and one-dimensional arrays are the same. In a two-dimensional array, A and a[0] all point to the first address of the array, and the two-dimensional arrays are stored in row order.
So two-dimensional arrays and two-dimensional pointers are vastly different, and the structure of two-dimensional arrays is basically the same as one-dimensional arrays . Thus, the process of passing a two-dimensional array to int** is not difficult to understand, as follows:
#include <iostream>using namespace Std;void fun (int. **a,int N) {for (int. i=0;i<n;i++) {for (int j= 0;j<n;j++) { cout<<* ((int*) a+i*n+j) <<endl;//Two-dimensional array a structure is essentially a one -dimensional array//passed in to be coerced into int**, The bottom layer becomes a two-level pointer structure is wrong, here to be forced into a layer of structure //due to the int** of the cast is wrong, so it cannot be used as a two-dimensional pointer, cannot use A[I][J] } }}void Main () { int test[2][2]={{1,2},{3,4}}; Fun (int * *) test, 2); Cin.get ();}
That's basically what I understand.
A two-dimensional array as a function parameter pass