Two-dimensional array in C ++ as function parameter

Source: Internet
Author: User

First, two-dimensional arrays are defined as follows:

A.

int Arr[2][3]={{1,2,3},{4,5,6}};

B.

int **Arr=new int* [2];for(int i=0;i<2;i++)    Arr[i]=new int[3];//initializefor(int i=0;i<2;i++)    for(int j=0;j<3;j++)        Arr[i][j]=i*3+j+1;

However, there is an important difference between the two methods: the two-dimensional arrays declared by method A are stored continuously by row, while the rows declared by Method B are not stored continuously!

For example, both methods can beArr [I] [j]Or* (Arr + I) + j)Access element, but method A can access the element through the following access method similar to the one-dimensional array

* (Arr [0] + I * cols + j)

This access method is incorrect for method B.


Using the row-based continuous storage of A two-dimensional array, you can convert A two-dimensional array into A one-dimensional pointer as A function parameter, and it has good scalability:

1. If we know that there is A two-dimensional array Arr defined by method A with rows and cols columns, we can declare it as A function Func parameter as follows:

Void Func (int * A, int rows, int cols) {for (int I = 0; I
 
  
We may also want to use two-dimensional pointers as function parameters instead of one-dimensional pointers, as shown in figure Void Func (int ** A, int rows, int cols );

In this caseFunc (Arr, 2, 3 );"Int (*) [3] type real participation in int ** type form parameters are not compatibleThat is, the parameter type does not match. Therefore, the two-dimensional array defined by method A can only be converted to one-dimensional pointer as the function parameter in the above way. Using two-dimensional pointers as function parameters this method applies to method B.


2. Because the two-dimensional array defined by Method B is not stored consecutively by row, the function parameter can only be a two-dimensional pointer, that is, the following statement

Void Func (int ** A, int rows, int cols) {for (int I = 0; I
   
    
In summary, to use A two-dimensional array as A function parameter, we must first specify that the two-dimensional array is defined in that way (A or B), and then adopt the corresponding function declaration method. The two function declaration methods have good scalability for different row and column numbers. The above is based on two-dimensional arrays, but the principles can be shared to three-dimensional and more multi-dimensional arrays, the processing method is the same.
    


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.