How to pass a two-dimensional array, reproduced from CSDN

Source: Internet
Author: User

a two-dimensional array is passed as a parameter (using a two-dimensional array to process the matrix), but the function that wants to accept a two-dimensional array parameter can handle an array of any dimension (the number of rows and columns of the matrix is not fixed).

"The following reprint"
----------------------------------------------------------------------------------------------
But the basic rule of passing a two-dimensional array is like this: You can use a two-dimensional array as an argument or a formal parameter, you can specify the size of all dimensions when defining a parameter group in the called function, or you can omit the size description of the first dimension. Such as:

    void Func (int array[3][10]);
    void Func (int array[][10]); The

is both legal and equivalent, but cannot omit the size of the second or higher dimension, as defined below is illegal :
     void Func (int array[][]); When the

treats a two-dimensional array as an argument, it must indicate all dimensions or omit the first dimension, but can't omit the size of the second dimension or the higher dimension , which is limited by the compiler principle. Learning how to compile this lesson knows that the compiler handles arrays like this:
for array int p[m][n], if you want to take p[i][j] values (i>=0 && i<m && 0<=j && J < N), which is addressed by the compiler with the following address:
     p + i*n + j;

As can be seen from the above, if we omit the size of the second dimension or higher, the compiler will not know how to properly address it. But when we write the program, we need to use a two-dimensional array of various dimensions are not fixed as a parameter, this is difficult, the compiler does not recognize Ah, how to do? Do not worry, although the compiler is not recognized, but we can not think of it as a two-dimensional array, but instead of it as a normal pointer, and then add two parameters to indicate the number of dimensions, and then we have to manually address the two-dimensional array, so that the two-dimensional array as a function of the parameter transfer, according to this idea, We can change the parameter of the dimension fixed to the parameter of the dimension, for example:

void Func (int array[3][10]);
void Func (int array[][10]);
Into:
void Func (int **array, int m, int n);

In the transformed function, Array[i][j] Such a formula is wrong (not believe, you can try it), because the compiler does not correctly address it, so we need to imitate the compiler's behavior of array[i][j] such a formula to manually turn into

* ((int*) array + n*i + j);

When calling such a function, it is important to note that, as in the following example:
int a[3][3] =
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3}
};
Func (A, 3, 3);

Depending on the different compiler settings, there may be warning or error, which can be cast as follows:
Func ((int**) A, 3, 3);
----------------------------------------------------------------------------------------------

Required (int**) casts, because two-dimensional arrays and two-level pointers are different, a is essentially an int (*a) [3], which is an array pointer, that is, a[0] is the address of the first-dimensional array's first element, A[1] is the address of the first element of the second-dimensional array, a[2] Is the address of the first element of the third-dimensional array, which is different from int**, and if it goes to int**, it loses the effect of a + i = a + i*3 like an array pointer.
And if you define a char *p[3], it is a one-dimensional array of pointers, at which point P is pointing to a pointer, not an array. Then if you define char **q = p, you can, and you can access the string through q[0],q[1].

Arrays and pointers This kind of thing is so cumbersome and complicated, personal humble opinion, in C + +, as far as possible to use STL, and can use template non-type parameters to solve this flexibility to deal with the non-fixed row number matrix function, effective C + + should have introduced, And there are optimizations for this template.

How to pass a two-dimensional array, reproduced from CSDN

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.