Pass Two Dim Array!

xiaoxiao2021-03-06  56

How will two-dimensional array passed as a parameter of the function: jatix-mail: jatix@163.com QQ: 52287017

statement:

If you are a big man, this article may waste your time, if you insist on see, I certainly feel very high.

Xing, but I hope that you will read it! If you find that my article has a mistake, you can ask criticism and

I will be happy to accept it. * _ *

Overview:

When you write a program today, you should use the two-dimensional number of groups to pass to a function, I found the two-dimensional number of group parameters.

Passing is not imagined, but in the end, I also solved the problems encountered, so this article mainly introduces

How to deal with the case of the two-dimensional array as a parameter transmission, I hope that everyone will not waste time on this.

text:

First of all, I quoted the original text of the "C-program Design" edited by Mr. Tan Haqiang, it briefly introduced how

Transfer two-dimensional array as a parameter, the original text is as follows (slight change, please forgive):

[Original start]

You can use a two-dimensional array name as a solid parameter or a metall parameter, and can be referred to when the array parameter group is defined in the called function.

The size of all dimensions can also be omitted the size of the first dimension, such as: Void Func (int Array [3]); Void func (int Array []); both are legal and Equivalent, but cannot save the second dimension or more dimensional size, as defined below is

Illegal: void func (int Array []); because of the start address of the array from the actual parameters, store the rules in the memory (stored by row),

And there is no district branch and column. If the number of columns are not explained in the shape, the system cannot determine how many columns should be, cannot

Only one-dimensional but not specified the second dimension, the following method is wrong: Void func (int Array [3]); the real parameter group dimension can be larger than the shape parameter group, such as the real parameter group is defined as

: Void func (int Array [3]); the shape parameter group is defined as: int Array [5] [10]; this time the array is only part of the parameter group, and the rest does not work.

[End]

As you can see, when two-dimensional array is used as a parameter, you must specify all dimensions size or omitted the first dimension.

However, it is not possible to omit the second dimension or more dimensional size, which is limited by the principle of compiler. Everyone is learning the principle of compilation

When such a course knows, the compiler is to handle arrays:

For array INT P [M] [N]; if you want to take the value of P [i] [j] (i> = 0 && i

The address is: p i * n j;

As can be seen from the above, if we omitted the second dimension or more dimensional size, the compiler will not know how to correct

Addressing. However, we need to use the two-dimensional array of each dimension as a parameter when writing a program.

This is difficult, the compiler can't recognize A. What should I do? Don't worry, although the compiler can't identify, but we

Can not take it as a two-dimensional array, but use it as a normal pointer, plus two parameters

Specifier each dimension, then we are handmade for the two-dimensional array, so that the parameters of the two-dimensional array as a function

The purpose of the pass, according to this idea, we can turn the dimension of the dimension to dimension to dimromes, for example: void func (int Array [3]); void func (int arch [] [10]) ; Becomes: void func (int ** array, int m, int N);

In the function after the transition, Array [i] [j] is wrong (do not believe, everyone can try it), because

The compiler cannot be properly addressed, so we need to imitate the behavior of the compiler to put array [i] [j].

Handmade transformation is

* (INT *) Array N * i j);

When calling such a function, you need to pay attention to, as follows: INT A [3] [3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3} }; Func (a, 3, 3);

Depending on the settings of different compilers, WARNING or ERROR may appear, and force conversion can be called as follows

: Func ((int **) a, 3, 3);

In fact, the multi-dimensional array and two-dimensional array principles are the same. You can expand the multidimensional array of yourself.

. Write here, I first saw this article to apologize, wasting your time. Here is a complete example

Subprogram, the main function of this example program is to ask a vertex to the shortest path to other vertices in the figure,

The form of adjacent matrices (that is, a two-dimensional array), in fact, this function is also useful, but we are this

The focus of article is to pass the two-dimensional array as a function of the function.

转载请注明原文地址:https://www.9cbs.com/read-118670.html

New Post(0)