How can I pass a multi-dimensional array as a parameter to a function? (Take a two-dimensional array as an example)

Source: Internet
Author: User

# Define X (3)
# Define y (5)

Int main (){
Int
D [x] [Y] = {1, 2, 3, 4, 5, 6, 7, 8 };
/* I want to print the content of d */
}

Then, we generally write as follows:

# Define X (3)
# Define y (5)

Int main (){
Int
D [x] [Y] = {1, 2, 3, 4, 5, 6, 7, 8};/* I want to print the content of d */
/* This is the implementation solution in main */
Int I, J;
For (I = 0; I <= X-1; I ++ ){
For (j = 0; j <= Y-1; j ++ ){
Printf ("% d,", d [I] [J]);
}
Printf ("\ n ");
}
Return 0;
}

Because we may often need to print a two-dimensional array, we would like to write a function specifically to do this:

# Define X (3)
# Define y (5)

/* To print d [] [] at any time, I plan to write the following function to do this */
/* Note the writing of this function parameter. Can I change it to int d [] [] or Int d [x] [] or Int d [x] [Y?
Think about it. Why? */
Void show (Int d [] [5]){
Int I, J;
For (I = 0; I <= X-1; I ++ ){
For (j = 0; j <= Y-1; j ++ ){
Printf ("% d,", d [I] [J]);
}
Printf ("\ n ");
}
Return;
}

Int main (){
Int
D [x] [Y] = {1, 2, 3, 4, 5, 6, 7, 8 };
/* I want to print the content of d */
/* This is the implementation solution using functions */
Show (d );
Return 0;
}

Let's look at another form of description:

 

# Define X (3)
# Define y (5)

/* Further derivation, You know, professionalProgramYou always like to write pointer parameters in function parameters,
Instead of int d [] [Y.
So we have the followingCode. */
Void show (Int * d) {/* See!Here is a pointer Parameter*/
Int I, J;
For (I = 0; I <= X-1; I ++ ){
For (j = 0; j <= Y-1; j ++ ){
Printf ("% d ,",D [I * Y + J]);/* See!Does this statement profoundly reflect the relationship between pointers and array elements?*/
}
Printf ("\ n ");
}
Return;
}

Int main (){
Int
D [x] [Y] = {1, 2, 3, 4, 5, 6, 7, 8 };
/* I want to print the content of d */
/* This is the implementation solution in main */
Show (d );
Return 0;
}

So far, readers can understand the meaning of the above Code.

All the code in this article can be downloaded here.

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.