In the process of writing C code, it is sometimes necessary to pass a multidimensional array to the tuned function in the keynote function, when the array needs to act as a parameter and an argument in the function being tuned. Here, I use myarray[3][3] as the array in question.
A number of examples have been given on the web to illustrate what happens when a call and the called function are in a source file. In this case, you do not need to specify the first subscript of the called array in the called function parameter, but you need to specify a second subscript, that is, the form myarray[][3].
However, when the call and the called function are not in a file, it needs to be handled in a specific way, given an example:
1 void Print_my_array (int (*myarray) [3 ]) 2 { 3 for (int i=0 ; i<= 2 ; ++i) { 4 printf ( "" , Myarray[i][0 ], Myarray[i][1 ], Myarray[i][2 5 } }
The tuned function is in file1.c and is declared in File1.h:
1 #ifndef __file1_h 2 #define __file1_h34 #include <stdio.h>56void Print_my_ Array (int (*) [3]); 7 8 #endif
When calling in the main function main, the arguments in the calling function do not need to use subscripts, directly using the array name.
1#include <stdio.h>2#include"File1.h"3 4 intMainintargcChar*argv[])5 {6 intmyarray[3][3];7 8 Pirnt_my_array (myarray);9 Ten return 0; One}
For the above example to be explained, the parameter in the modulated function uses the form of int (*myarray) [3] , which explicitly expresses the multidimensional array as an array of pointers. The benefit of this approach is that it is easy to declare with void print_my_array (int (*) [3]) in the declaration.
About the declaration, definition, and invocation of a multidimensional array as a function parameter