About the declaration, definition, and invocation of a multidimensional array as a function parameter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.