Two-dimensional array names are used as real parameters or parameters, and two-dimensional arrays are used

Source: Internet
Author: User

Two-dimensional array names are used as real parameters or parameters, and two-dimensional arrays are used


 
1. method 1. void fun (int a [4] [6]); // specify the number of columns of a two-dimensional array when two-dimensional data is used as a function parameter.
 
Void fun (int a [] [6]) // The number of two-dimensional array rows can be omitted.
 
In addition, two-dimensional arrays cannot be defined as their subscripts using constants.
 
*/

Void fun1 (int a [] [3], int n) {// number of rows in the array
For (int I = 0; I <n; I ++ ){
For (int j = 0; j <3; j ++ ){
Printf ("% d", a [I] [j]);
}
}
}


/*
 
 
 
Method 2. void fun (int (* pArray) [6]) applies a one-dimensional pointer group as a parameter


*/

Void fun2 (int (* arr) [3], int n ){
For (int I = 0; I <n; I ++ ){
For (int j = 0; j <3; j ++ ){
Printf ("% d", arr [I] [j]);
}
}
}



/*


Method 3. void fun (int (& arr) [10] [10]) applies a two-dimensional array reference as a parameter. Two dimensions must be specified here.

Method 4. void fun (int (* arr) [10] [10]) uses a two-dimensional pointer array as a parameter. Two dimensions must be specified here.

Note: The above four methods are common, but when the array is passed, only the value is passed, and the input parameter is not changed.



Method 5. void fun (int ** arr, int m, int n) uses the double pointer as the shape parameter. The passed real parameter must also be a double pointer, cleverly allocating memory space using new, can be defined with a limit

Arr [I] [j] is wrong to get the value, because the compiler cannot properly address it, so we need to simulate the behavior of the compiler to convert the formula such as arr [I] [j]

Manually convert to * (int *) arr + n * I + j );



Related Article

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.