Source: https://www.cnblogs.com/weixiaoyu/p/4371806.html
1. For a one-dimensional array, the array is passed as a function parameter, actually passing a pointer to the array, in the C compiler, when the array name is used as a function parameter, the array name in the function body is automatically degraded to a pointer. When a function is called, the value of the array element is changed by the equivalent of a pass-through instead of a value.
For example: void fun (int a[]); If there are a[i]++ in the fun function, then the corresponding array elements are modified, and the call is made with fun (a);
2, for high-dimensional arrays, you can use the two-dimensional array as an argument or a formal 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 fun (int array[3][10]);
void fun (int array[][10]);
Both are legal and equivalent, but cannot omit the size of the second or higher dimension, as the following definition is illegal:
void fun (int array[][]);
Because arguments are passed from the beginning address of the array, stored in memory by array rules (stored in rows), not branches and columns, if the number of columns is not specified in the formal parameter, the system cannot determine how many rows should be, and cannot specify only one dimension without specifying a second dimension, the following is wrong:
void fun (int array[3][]);
The real parameter group dimension can be greater than the shape parameter group, for example, the shape parameter group is defined as:
void fun (int array[3][10]);
The real parameter group is defined as:
int array[5][10];
The shape parameter group only takes part of the real parameter group, and the remainder does not work.
As you can see, when you use a two-dimensional array as an argument, you must indicate all dimensions or omit the first dimension, but you cannot omit the size of the second or higher dimension, which is limited by the compiler principle. When you learn the principles of compiling, you should know that the compiler handles arrays like this:
for array int p[m][n];
If you want to take the value of P[i][j] (i>=0 && i<m && 0<=j && J < N), the compiler is addressing this, and its address is:
P + i*n + j;
As can be seen from the above, if we omit the size of the second dimension or higher, the compiler will not know how to properly address it, meaning that the N value here is clearly known when the parameter is defined. But when we write the program, we need to use a two-dimensional array of various dimensions are not fixed as a parameter, this is difficult, the compiler does not recognize Ah, how to do? Do not worry, although the compiler is not recognized, but we can not think of it as a two-dimensional array, but instead of it as a normal pointer, and then add two parameters to indicate the number of dimensions, and then we have to manually address the two-dimensional array, so that the two-dimensional array as a function of the parameter transfer, according to this idea, We can change the parameter of the dimension fixed to the parameter of the dimension, for example:
void fun (int array[3][10]);
void fun (int array[][10]);
Into:
void Fun (int **array, int m, int n);
In the transformed function, Array[i][j] Such a formula is wrong (not believe, you can try it), because the compiler does not properly address it, so we need to imitate the compiler's behavior of array[i][j] such a formula to manually change to:
* (*array + n*i + j);
When calling such a function, it is important to note that, as in the following example:
int A[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
Fun (A, 3, 3);
Depending on the different compiler settings, there may be warning or error, which can be cast as follows:
Fun ((int**) A, 3, 3);
Use of arrays as formal parameters