How to pass a two-dimensional array as a parameter to a function in C Language
The problem of passing function parameters of two-dimensional arrays has been solved several times before, but I still forget it. This is summarized here.
# Include <stdio. h>/********************************** Method 1: the length of the first dimension can be equal to or equal to the length of the second dimension ************************ * ********/void print_a (int A [] [5], int N, int m) {int I, j; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) printf ("% d", a [I] [J]); printf ("\ n ");}} /*************************************** * ** Method 2: ****************************** * ***********/void print_ B (INT (*) [5], int N, int m) {int I, j; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) printf ("% d", a [I] [J]); printf ("\ n ");}}/************************************ Method 3: using arrays as the feature of sequential storage, ** you can use dimensionality reduction to access the original array! * **********************************/Void print_c (int *, int N, int m) {int I, j; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) printf ("% d", * (a + I * m + J); printf ("\ n ");}} Int main (void) {int A [5] [5] = {1, 2}, {3, 4, 5}, {6}, {7 }, {0, 8 }}; printf ("\ n Method 1: \ n"); print_a (A, 5, 5); printf ("\ n Method 2: \ n "); print_ B (A, 5, 5 ); Printf ("\ n method 3: \ n"); print_c (& A [0] [0], 5, 5 ); Getch (); Return 0 ;}
Method 3 can be used to handle the problem that the parameter XX [] [] cannot be converted to XX ~