Passing two-dimensional arrays as parameters in Functions

Source: Internet
Author: User

First, void f (a [] []); is incorrectly defined.

When two-dimensional data is used as a function parameter, you must specify the number of columns in the two-dimensional array so that the compiler can understand how to address it.

The correct method is void f (a [4] [6]).

Void f (a [] [6]); // The number of rows in a two-dimensional array can be omitted.

In addition, a two-dimensional array cannot be defined as its subscript with a very large number.

Void f (a [m] [N]); is a serious error;


Correct Application Mode 1

Void func1 (INT iarray [] [10])
{

}

Int main ()
{
Int array [10] [10];
Func1 (array );

}

Correct Application Mode 2 (using a one-dimensional pointer array as a parameter)

Void func2 (int
(* Parray) [10])
{

}

Void func2_1 (INT (* parray) []) // The code is compiled and cannot be called.
{

}

Int main ()
{
Int array [10] [10];
Func2 (array );
}

Correct Application Mode 3 (using two-dimensional array references as parameters)

Void func3 (INT (& parray) [10] [10]) // two dimensions must be specified here
{

}

Int main ()
{
Int array [10] [10];
Func3 (array );
}

Correct Application Mode 4 (using a two-dimensional pointer array as a parameter)

Void func4 (INT (* parray) [10] [10]) // you must specify two dimensions.
{

}

Int main ()
{
Int array [10] [10];
Func4 (& array );

}

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.

Supplement: When the double pointer is used as the form parameter, the passed real parameter must also be a double pointer, cleverly allocating memory space using new, which can be defined with a very large volume

Void func5 (INT ** parray, int M, int N)
{

}

# Include <ctime>
Int main ()
{
Int M = 10;
Int n = 10;


Int ** parray = new int * [m];
Parray [0] = new int [M * n];
// Allocate continuous memory and dynamically create two-dimensional arrays

// Parray [1] [0] cannot be used for addressing. You must specify the subscript addressing mode.
For (INT I = 1; I <m; I ++)
{
Parray [I] = parray [I-1] + N;
}

Func5 (parray, m, n );
}



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.