Two-dimensional arrays are passed as parameters

Source: Internet
Author: User

Example of two-dimensional array parameter #include <iostream> using namespace std; Method 1: Pass the array, note that the second dimension must be marked void fun1 (int arr[][3],int irows) {for (int i=0;i<irows;i++) {for (int j=0;j&          lt;3;j++) {cout<<arr[i][j]<< "";      } cout<<endl;  } cout<<endl; }//method 2:1 heavy pointer void fun2 (int (*arr) [3],int irows) {for (int. i=0;i<irows;i++) {for (int j=0;j&          lt;3;j++) {cout<<arr[i][j]<< "";      } cout<<endl;  } cout<<endl;          }//Method Three: pointer passing, whether it is a few-dimensional array that sees him as a pointer, void Fun3 (Int*arr,int irows,int icols) {for (int i=0;i<irows;i++) {          for (int j=0;j<3;j++) {cout<<* (arr+i*irows+j) << "";      } cout<<endl;  } cout<<endl;      } int main () {int a[2][3]={{1,2,3},{4,5,6}};      FUN1 (a,2);      cout<<endl;      Fun2 (a,2);      cout<<endl; HereCast must be forced, because a is a two-dimensional array, and the need to pass in is a pointer//and must be cast to a pointer, if a is a one-dimensional array then you do not have to force the type conversion/why a one-dimensional array does not have to be cast and the two-dimensional array must be converted, this problem is not resolved,      Fun3 ((int*) a,2,3);  cout<<endl;   }

  

Do you accept two-dimensional array arguments with a double pointer int** as a formal parameter? The answer is yes, but it's limited. With a double pointer as a formal parameter, the corresponding argument is also a double pointer. In fact, this double pointer actually points to an array of pointers, the way the double pointers are declared, and is ideal for passing a dynamically created two-dimensional array. How to create a two-dimensional array dynamically? The following programs:

    1. int main ()
    2. {
    3. int m = 10;
    4. int n = 10;
    5. int** p = new int[m][n];
    6. }

You will find that the compilation does not pass, and the second dimension must be a constant. So how do you declare a two-dimensional array with two dimensions that can be dynamically specified? See below:

  1. void Func5 (int** parray, int m, int n)
  2. {
  3. }
  4. #include <ctime>
  5. int main ()
  6. {
  7. int m = 10;
  8. int n = 10;
  9. int** Parray = new int* [M];
  10. Parray[0] = new int[m * n]; //Allocate contiguous memory
  11. //With parray[1][0] is not addressable and you need to specify the subscript addressing method
  12. For (int i = 1; i < m; i++)
  13. {
  14. Parray[i] = parray[i-1] + N;
  15. }
  16. Func5 (Parray, M, N);
  17. }

This applies a contiguous amount of memory for the two-dimensional array, and then assigns each element an addressing method (you can also request memory for each element, you do not have to specify the addressing method), and finally passes the double pointer as an argument to Func5. Here func5 more than two parameters, is the dimension of the two-dimensional array, you can also not declare these two parameters, but for the sake of security, still specified good. Finally compile, run, everything OK. To summarize, the above code is actually a two-dimensional array that implements the dynamic creation of parameter passing.

Two-dimensional arrays are passed as parameters

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.