The study content of this article reference: http://blog.csdn.net/yunyun1886358/article/details/5659851
http://blog.csdn.net/xudongdong99/article/details/6723163
1. Use a two-dimensional array as an example of formal parameters:
void func (int arr[][]) { } int main () { int array[] [ten]; Func (array); // use a two-dimensional array to be an argument, call a function }
The above example can be compiled through, note, here must give the size of the second dimension , otherwise the compilation will not pass.
2. Use a one-dimensional pointer as a function parameter in the following form:
void func1 (int (*arr) [ten]) {}int main () { int array[] [ten]; Func1 (array); }
This declaration is also ok, because the two-dimensional array name is a pointer to a one-dimensional array, in the same way, it must also indicate the size of the second dimension , otherwise the compilation will not pass.
3. Use double pointers as formal parameters: the parameters here specify the dimensions of each dimension of the two-dimensional array
voidFunc2 (int* * parr,intMintN) {}intmain () {intm =Ten; intn =Ten; int* * Parray =New int*[m]; parray[0] =New int[m * n];//Allocating contiguous memory//cannot be addressed with parray[1][0] and you need to specify the subscript addressing method for(inti =1; I < m; i++) {parray[i]= parray[i-1] +n; } func2 (parray, m, n); }
4.Demo (from the race code NETWORK)
Question:
There are n squares, left-to-right in a row, numbered 1-n.
There are 3 types of operation for M operations:
1. Modify the weights of a lattice,
2. to seek a continuous lattice of weights and
3. Ask for the maximum value of a continuous lattice.
For each of the 2, 3 operation output you have to find the Results.
|
Input:
Enter the first row of two integers, N for the number of squares, m for the number of operations, N and M in the middle with a space separated;
Next Enter n lines, one integer per line representing the weights of a lattice
Next enter M lines, each line has three integers, the middle is separated by a space; the first is the selected operation type 1-3, and the second and third integers are the number of the Operation Lattice. |
Input Example:
7 ·
7
8
9
2 1 3
3 1 3
2 1 2 |
|
Output:
No output if 1 operation is performed
Outputs an integer if the 2 and 3 operations are performed |
Sample Output:
24
9
15 |
Solution: using a double pointer as an example of a function parameter, the argument in this example is a dynamically created Double-pointer form that can be passed directly as an argument, and if the argument is just a normal two-dimensional array, it is necessary to do the corresponding type conversion when doing the Argument.
#include <iostream>using namespacestd;//operation result output functionvoidOperatortype (intweight[],intNint**operatorarr,intM) {intsum =0; intmax=0; Switch(operatorarr[m][0]) { case 1: break; case 2: for(inti = operatorarr[m][1];i <= operatorarr[m][2];i++) {sum+ = weight[i-1]; } cout<< sum <<endl; break; case 3: for(inti = operatorarr[m][1];i <= operatorarr[m][2];i++) {max=0.0; if(max < weight[i-1]) Max= weight[i-1]; } cout<< Max <<endl; break; default: break; }}intmain () {intn, m;//n means the number of squares m indicates the number of operationsCIN >> N >>m; int*weight =New int[n];//the weights of each lattice int**oper =New int*[m];//an array of two-dimensional arrays that hold operands and grid numbers for(inti =0; I < m;i++) {oper[i]=New int[3]; } for(inti =0; I < N;i++)//weight of the input grid{cin>>weight[i]; } for(inti =0; I < M;i++)//input Operation Array { for(intj =0; J <3; j + +) Cin>>oper[i][j]; } for(inti =0; I < m;i++) {operatortype (weight, n, oper, i); } System ("Pause"); return 0;}
C + + two-dimensional arrays (double pointers as function Parameters)