Array parameter passing is very common. When passing an array in C/C ++, the array name degrades to a pointer, so the first address and length are generally given. This is flawed. What should we do if we do not know the dimensions when writing a function?
The following uses matrix multiplication as an example and provides three implementation methods: GP, flattening the array, struct
Among them, I think flattening the array method is the best. The glmaplf function in OpenGL uses this method. Its function prototype is as follows:
void glMap1f(GLenum target, GLfloat uMin, GLfloat uMax, GLint stride, GLint order, const GLfloat *controlPointsPtr);
For more information about parameters, see http://books.google.com.bd/books? Id = lbm1khtux0cc & Pg = pa178 & LPG = pa178 & DQ = glmaplf & source = bl & OTs = kor3v4d1dm & Sig = sUtOqv6JPNsFIm4Fmo6K-SjwC70 & HL = ZH-CN & SA = x & Ei = 9uqqualiosh9laxo7ycidw & ved = 0cc0q6aewaa # V = onepage & Q = glmaplf & F = false
I. source program
# DEFINE _ crt_secure_no_warnings # include <iostream> using namespace STD; const int max_row = 10; const int max_column = 10; Template <class T> struct matrix {unsigned int row; unsigned int column; t m [max_row] [max_column]; matrix (unsigned int _ row, unsigned int _ column): Row (_ row), column (_ column) {memset (M, 0, sizeof (M) ;};// gptemplate <unsigned int X, unsigned int y, unsigned int Z, class T> void gpmatrixm Ultiply (t a [x] [Y], t B [y] [Z], T c [x] [Z]) {for (INT I = 0; I <X; ++ I) // compare the number of signed characters with the number of unsigned characters. No warning is given for why? {For (Int J = 0; j <z; ++ J) {C [I] [J] = 0; For (int K = 0; k <Y; ++ K) {C [I] [J] + = A [I] [k] * B [k] [J] ;}}} // flattening the arraytemplate <class T> void flattenmatrixmultiply (T * P, T * q, T * r, unsigned int X, unsigned int y, unsigned int Z) {for (unsigned int I = 0; I <X; ++ I) {for (unsigned Int J = 0; j <z; ++ J) {R [I * z + J] = 0; For (unsigned int K = 0; k <Y; ++ K) {R [I * z + J] + = P [I * Y + k] * Q [K * z + J] ;}}} // structtemplate <class T> matrix <t> structmatrixmultiply (matrix <t> A, matrix <t> B) {matrix <t> C (. row, B. column); For (unsigned int I = 0; I <. row; ++ I) {for (unsigned Int J = 0; j <B. column; ++ J) {C. M [I] [J] = 0; For (unsigned int K = 0; k <. column; ++ K) // to be detected. column = B. row {C. M [I] [J] + =. M [I] [k] * B. M [k] [J] ;}} return C ;}# input/output function template defined by Pragma region <class T> istream & operator> (istream & is, const matrix <t> & matrix) {for (unsigned int I = 0; I <matrix. row; ++ I) {for (unsigned Int J = 0; j <matrix. column; ++ J) {is> T (matrix. M [I] [J]) ;}} return is ;}template <class T> ostream & operator <(ostream & OS, const matrix <t> & matrix) {for (unsigned int I = 0; I <matrix. row; ++ I) {for (unsigned Int J = 0; j <matrix. column; ++ J) {OS <matrix. M [I] [J] <";}cout <Endl ;}return OS ;}template <unsigned int row, unsigned int column, class T> void inputmatrix (T matrix [row] [column]) {for (INT I = 0; I <row; ++ I) {for (Int J = 0; j <column; ++ J) {CIN> matrix [I] [J] ;}}template <unsigned int row, unsigned int column, class T> void outputmatrix (T matrix [row] [column]) {for (INT I = 0; I <row; ++ I) {for (Int J = 0; j <column; ++ J) {cout <matrix [I] [J] <"" ;}cout <Endl ;}} # input/output functions defined by Pragma endregion int main (INT argc, char ** argv) {freopen ("cin.txt", "r", stdin); const unsigned int ROWA = 2; const unsigned int Columna = 3; // Columna must be equal to rowb const unsigned int rowb = 3; const unsigned int columnb = 3; int A [ROWA] [Columna]; int B [rowb] [columnb]; int C [ROWA] [columnb]; // gpcout <"GP" <Endl; inputmatrix <ROWA, Columna> (); inputmatrix <rowb, columnb> (B); cout <"A =" <Endl; outputmatrix <ROWA, Columna> (a); cout <Endl; cout <"B =" <Endl; outputmatrix <rowb, columnb> (B); cout <Endl; gpmatrixmultiply <ROWA, Columna, columnb> (A, B, c); // a constant is required. cout <"a * B =" <Endl; outputmatrix <ROWA, columnb> (c); cout <Endl; is determined during compilation; // flattening the arraycout <"flattening the array" <Endl; inputmatrix <ROWA, Columna> (a); inputmatrix <rowb, columnb> (B ); cout <"A =" <Endl; outputmatrix <ROWA, Columna> (a); cout <Endl; cout <"B =" <Endl; outputmatrix <rowb, columnb> (B); cout <Endl; flattenmatrixmultiply (* a, * B, * C, ROWA, Columna, columnb ); // output matrix <ROWA, columnb> (c); cout <Endl; // structcout <"struct" <Endl; matrix <int> Ma (ROWA, columna); matrix <int> MB (rowb, columnb); CIN> Ma> MB; cout <"A = \ n" <MA <Endl; cout <"B = \ n" <MB <Endl; cout <"a * B = \ n" <structmatrixmultiply (MA, MB) <Endl; return 0 ;}
Ii. Test Data
1 2 02 1 32 3 01 -2 -13 1 10 0 02 1 32 3 01 -2 -13 1 11 2 02 1 32 3 01 -2 03 1 0
Iii. Running results
GPA = 1 2 02 1 3B = 2 3 01-2-13 1 1a * B = 4-1-214 7 2 flattening the arraya = 0 0 02 1 3B = 2 3 01 -2-13 1 10 0 014 7 2 structa = 1 2 02 1 3B = 2 3 01-2 03 1 0a * B = 4-1 014 7 0 press any key to continue ...