How to pass the two-dimensional array as a function of the function

xiaoxiao2021-03-06  94

http://blog.9cbs.net/jatix/archive/2004/08/29/Use_of_array.aspx

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 hand-made for the two-dimensional array, so that the purpose of passing the two-dimensional array as a function of the function, according to this idea, we can turn the dimension of the dimension to the dimension of the dimension. ,E.g:

Void func (int Array [3]); Void func (int Array []); vocal 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.

The complete example program includes three files, which is debugged under Microsoft Visual C 6.0. as follows:

// // mian.c is the main program entry, and calls the demonstration how to call a vertex to other vertices in a picture.

The shortest road // function // #include "short.h" #include int main () {INT i = 0, v = 0; float adjoinmatrix [5] [5] = {0 , 10, NO_PATH, 30, 10, 50, NO_PATH, NO_PATH}, {NO_PATH, NO_PATH, 0, NO_PATH, 10}, {NO_PATH, NO_PATH, 20, 0, 60}, {NO_PATH, NO_PATH, NO_PATH , NO_PATH, 0}}; int path [5]; float length [5]; ShortestPath (Adjoinmatrix, Length, Path, 5, 0); for (i = 1; i <5; i ) {v = i; while (v! = 0) {Printf ("% d", v); v = path [v];} Printf ("% d / n", v);} return 0;}

// // ShortestPath.h defines a function of seeking a vertex to other vertices in a picture, and still set

Rightening a // macro, # define no_path 0x00ffffff, if the length of the direct path between the two vertices in the figure is

NO_PATH, / / ​​/ / indicates that the two vertices in the figure are not directly connected. // # iFNDef INCLUDE_SHORTESTPATH_H #define include_shortestpath_h #define in #define out #define no_path 0x00fffffffff / * Abstract: The function of this function is to find a vertex in the figure to the shortest path of all the other vertices, and its most

The length of the short circuit RETUREN value: Type is int

Path [N], Length [N] (n is the number of top points, // You can call as follows: ShortestPath (Adjoinmatrix,

LENGTH, PATH, 5, 0); // After the call, the shortest path is stored in Path [N], and the LENGTH [N] is in the most

In the length of the short path //, we have the shortest pass between the 0 vertex to other fixed-point.

Its length float adjoinmatrix [5] [5] = {{0, 10, NO_PATH, 30, 100}, {NO_PATH, 0, 50, NO_PATH, NO_PATH}, {NO_PATH, NO_PATH, 0, NO_PATH, 10}, {NO_PATH, NO_PATH , 20, 0, 60}, {_PATH, NO_PATH, NO_PATH, NO_PATH, 0}}; int path [5]; float length [5]; ShortestPath (AdjoinMatrix, Length, Path, 5, 5, 5,

0); INT i = 0, int V = 0; for (i = 1; i <5; i ) {v = i; while (v! = 0) {printf ("% d", v); v = PATH [V];} Printf ("% D / N", V);} - * / int shortestPath (in float ** adjoinmatrix, // stores the neighbor matrix of map, is

A two-dimensional array Out float * length, // is used to return to each

The shortest path of the shortest path of the point Out Int * path, // is used to return the shortest

Road and pass, PATH [i] indicates the number in int vertexnum, // vertex in the shortest road, the number of in int vertex // starting top points); #ENDIF

// // ShortestPath.c implements a function of the shortest path to other vertices in the figure. // #include "ShortestPath.h" #include / * Abstract: The function of this function is to find a vertex in the figure to the shortest circuit, and the shortest

The length of the path of the RETUREN value: Type is int, meaning the following 0 Success 1 resources are not enough - * / int shortestPath (in float ** AdjoinMatrix, // The adjacency matrix of the map is

A two-dimensional array Out float * length, // is used to return to each

The shortest path of the shortest path of the point Out Int * path, // is used to return the shortest

Road and pass, Path [i] means the vertex in INT VERTEXNUM, / / ​​vertex in the shortest road Top I INT Vertex // Started top Point) {INT i = 0, J = 0, W = 0 // / / The collection of points already in the shortest circuit, if VertexSet [i] is not 0, then the i-th point is in this collection // int * Vertexset = (int *) malloc (vertexnum); IF (vertexset == null) {return 1; // lack of memory resources} // // Initialization // for (i = 0; i

i); VertexSet [i] = 0; if (i! = vertex && length [i]

U * VERTEXNUM W)

w)

* (Float *) AdjoinMatrix U * VertexNum W); Path [W] = u;}}} Return 0;

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

New Post(0)