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

Source: Internet
Author: User

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

Today, when writing a program to use a two-dimensional array as a parameter to a function, I found that the two-dimensional array as parameters to pass it is not so simple, but finally I also solved the problem encountered, so this article mainly describes how to deal with the two-dimensional array as a parameter of the case, I hope we don't waste any more time on this.

Body:

First of all, I quoted the original section of "C program design" written by Mr. Rectification, which briefly describes how to

The two-dimensional array is passed as a parameter, as follows (slightly changed, please forgive):

[Beginning of the original]

You can use a two-dimensional array masterpiece as an argument or a parameter, you can specify the size of all dimensions when defining the 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]);

Both are legal and equivalent, but cannot omit the size of the second or higher dimension, as the following definition is illegal:

void Func (int array[][]);

Because arguments are passed from the beginning address of the array, stored in memory by array rules (stored in rows), not branches and columns, if the number of columns is not specified in the formal parameter, the system cannot determine how many rows should be, and cannot specify only one dimension without specifying a second dimension, the following is wrong:

void Func (int array[3][]); The real parameter group dimension can be greater than the shape parameter group, for example, the real parameter group is defined as:

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

The shape parameter group is defined as:

int array[5][10];

The shape parameter group only takes part of the real parameter group, and the remainder does not work.

[end of original text]

As you can see, when you use a two-dimensional array as an argument, you must indicate all dimensions or omit the first dimension, but you cannot omit the size of the second or higher dimension, which is limited by the compiler principle. When you're learning how to compile this course, you know that the compiler handles arrays like this:

for array int p[m][n];

If you want to take the value of P[i][j] (i>=0 && i<m && 0<=j && J < N), the compiler is addressing this, and its address is:

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 properly address it, so we need to imitate the compiler's behavior of array[i][j] such a formula to manually change to:

* ((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);

In fact, the multidimensional array and the two-dimensional array principle is the same, you can expand their own multidimensional array, here no longer repeat. Write here, I first to read this article regret the person to apologize, wasting your time. The following is a complete example of the program, the main function of this example is to find a vertex to the other vertex of the shortest path, the graph is stored in the form of adjacency matrix (that is, a two-dimensional array), in fact, this function is very useful, but the focus of our article is the two-dimensional array as a function of the parameters.

How to use a 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.