How to pass a two-dimensional array as a function parameter

Source: Internet
Author: User

How to pass a two-dimensional array as a function parameter

When writing a program today, we need to use a two-dimensional array as a parameter to pass it to a function. I found that passing a two-dimensional array as a parameter is not as simple as we thought, however, I finally solved the problem. Therefore, this article mainly introduces how to process two-dimensional arrays as parameter transmission. I hope you will not waste time on this.

Body:

 

First of all, I cited the original article in the C Programming Section prepared by Mr. Tan haoqiang. It briefly introduces how

Pass the two-dimensional array as a parameter. The original text is as follows (slightly changed, please forgive me ):

 

[Start with the original article]

The two-dimensional array name can be used as the real parameter or form parameter. when defining the form parameter array in the called function, you can specify the size of all dimensions or omit the size description of the first dimension. For example:

Void func (INT array [3] [10]);

Void func (INT array [] [10]);

Both of them are valid and equivalent, but the two-dimensional or higher-dimensional sizes cannot be omitted. the following definition is invalid:

Void func (INT array [] []);

Because the starting address of the array is passed from the real parameter, it is stored in the memory according to the array arrangement rules (stored by row), without the partition and column. If the column number is not specified in the form parameter, the system cannot determine how many rows and columns should be. It cannot specify only one dimension rather than the second dimension. The following statement is incorrect:

Void func (INT array [3] []); the dimension of the real parameter array can be greater than that of the form parameter array. For example, the real parameter array is defined:

Void func (INT array [3] [10]);

The parameter array is defined:

Int array [5] [10];

At this time, the form parameter array only takes part of the real parameter array, and the rest does not work.

[End of original article]

As you can see, when using a two-dimensional array as a parameter, you must specify the size of all dimensions or omit the first dimension, but do not omit the size of the second or higher dimensions, this is restricted by Compiler Principles. When learning the Compilation Principle Course, we know that the compiler processes arrays like this:

For the array int P [m] [N];

If you want to obtain the value of P [I] [J] (I> = 0 & I <M & 0 <= J & J <n ), the address of the compiler is as follows:

P + I * n + J;

From the above, we can see that if we omit the second-dimensional or higher-dimensional size, the compiler will not know how to address it correctly. However, when writing a program, we need to use a two-dimensional array with non-fixed dimensions as a parameter. This is hard to solve and the compiler cannot recognize it. What should we do? Don't worry. Although the compiler cannot recognize it, we can treat it as a two-dimensional array instead of a normal pointer, and add two parameters to specify the dimensions, then we manually address the two-dimensional array, so that we can pass the two-dimensional array as a function parameter. Based on this idea, we can change the fixed-dimension parameter to the instant parameter of the dimension, for example:

 

Void func (INT array [3] [10]);

Void func (INT array [] [10]);

Changed:

Void func (INT ** array, int M, int N );

 

In the transformed function, the formula "array [I] [J]" is incorrect (you can try it if you don't believe it), because the compiler cannot properly address it, therefore, we need to simulate the compiler behavior to manually convert a sub-statement like array [I] [J]:

* (Int *) array + N * I + J );

When calling such a function, pay attention to the following example:

Int A [3] [3] =

{

{1, 1, 1 },

{2, 2, 2 },

{3, 3, 3}

};

Func (A, 3, 3 );

 

Depending on different compiler settings, a warning or error may occur. You can perform the following forced conversion call:

Func (INT **) A, 3, 3 );

In fact, the principle of multi-dimensional arrays is the same as that of two-dimensional arrays. You can expand multi-dimensional arrays by yourself. Here, I apologize to those who have read this article and have wasted your time. The following is a complete example program. The main function of this example program is to find the shortest path from a vertex to another vertex in a graph, the graph is stored in the form of an adjacent matrix (that is, a two-dimensional array). In fact, this function is quite useful, but the focus of this article is to pass the two-dimensional array as a function parameter.

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.