How to pass a two-dimensional array as a parameter to a function in C Language

Source: Internet
Author: User
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 ~

 
 

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.