The two-dimensional array function parameters are as follows:
/ for a m row n column int element of the two-dimensional array // function f formal Parameter form f (int daytab[m][n]) {...} // the following two types of rows can be ignored (f (int daytab[][n]) {...} F (int (*daytab) [n]) {...}
It is not possible to ignore the size of the second dimension. The main reason is that the memory allocated by the two-dimensional array in the stack is contiguous, each line of it has the same element, so that array[i][j] and * (array +i) +j are the same, the program is aware that array+i I actually offsets i*n units, This also results in a two-dimensional array of array[3][3], using subscript array[2][1] and array[1][4] as the same element of access, although the latter's subscript is illegal for a 3*3 matrix, but this does not affect access.
Represents a two-dimensional array with pointers to a pointer, dynamically allocating the form of memory:
Such a method can make the size of the second dimension (that is, each row) not necessarily equal, strictly speaking, is not a two-dimensional array, but there are corresponding benefits. The memory allocated in this way can be directly accessed with a pointer pointer as a function parameter, and in the child function with double subscript.
#include <iostream>#include<stdio.h>void out(Double**a,intMintN) { intI, J; Doubleb=0.0; for(i=0; i<m; i++) { for(j=0; j<n; J + +) {A[i][j]=b; b+=1.2; printf ("%5.1f", A[i][j]); } std::cout<<Std::endl; } }intMainintargcChar*AGRV) { intI, J, m=2, n=3; Double**A; A=New Double*[M]; for(i=0; i<m; i++) A[i]=New Double[n]; out(A,m,n); return 1;}
C + + Two-dimensional arrays do function parameters