Two-dimensional array parameter transfer method

Source: Internet
Author: User
// Array. CPP // parameter transfer of two-dimensional arrays // 2013/6/28 # include <iostream> using namespace STD; // array name as the form parameter void fun1 (INT iarray [] [3]) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) cout <iarray [I] [J] <""; cout <Endl ;} // One-dimensional array pointer as the form parameter void fun2 (INT (* parray) [3]) {/* cout <parray <Endl; cout <& parray <Endl; cout <* parray <Endl; cout <** parray <Endl; cout <* (parray + 1) + 1) <Endl; cout <* parray + 1 <en DL; */For (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) cout <parray [I] [J] <"; // cout <* (parray + I) + J) <""; cout <Endl ;}cout <Endl ;}// two-dimensional array reference as the form parameter void fun3 (INT (& parray) [3] [3]) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) cout <* (parray + I) + J) <""; cout <Endl ;} // two-dimensional array pointer as the form parameter void fun4 (INT (* parray) [3] [3]) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) cout <(* parray) [I] [J] <" "; Cout <Endl ;}cout <Endl ;}// use a double pointer as the form parameter void fun5 (INT ** parray, int M, int N) {for (INT I = 0; I <m; I ++) {for (Int J = 0; j <n; j ++) cout <* (parray + I) + J) <""; cout <Endl ;}cout <Endl ;}int main (INT argc, char * argv []) {int A [3] [3] = {, 1}, {, 2}, {, 3 }}; // The array name is used as the form parameter fun1 (a); // the one-dimensional array pointer is used as the form parameter fun2 (a); // the two-dimensional array reference is used as the form parameter fun3 (); // point to the two-dimensional array pointer as the form parameter fun4 (& A); int (* aa) [3] [3] = & A; For (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) c Out <AA [I] [J] <"; cout <Endl ;}// use a double pointer as the form parameter // Wrong answer! /* Int M = 3; int n = 3; int ** P = new int [m] [N]; */int m = 3; int n = 3; int ** parray = new int * [m];/* parray [0] = new int [M * n]; // allocate continuous memory // parray [1] [0] is not addressable. You also need to specify the subscript addressing mode for (INT I = 1; I <m; I ++) {parray [I] = parray [I-1] + N ;}for (INT I = 0; I <m; I ++) for (Int J = 0; j <N; j ++) parray [I] [J] = I + 1; */For (INT I = 0; I <m; I ++) parray [I] = new int [N]; for (INT I = 0; I <m; I ++) for (Int J = 0; j <N; j ++) parray [I] [J] = I + 1; fun5 (parray, m, n); Return 0 ;}

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.