Question about using one-dimensional or multi-dimensional arrays as function parameters

Source: Internet
Author: User

Multi-dimensional arrays in C language cannot be used as parameters because they are restricted:

/* =====================================================
This program is a test program used for learning.
This section describes some learning mistakes in C language.
Detailed explanation of knowledge
======================================================= */
# Include <stdio. h>
# Include <stdlib. h>

Void fun1 (int m [2] [3])
{
Printf ("the first test value is % d/N", M [0] [1]);
}

Void fun2 (int * m, int weight, int height)
{
Printf ("second test value: % d/N", M [weight * 1 + 2]);
}
Void main ()
{
/* ===================================================== ======================
Topic: Question about using one-dimensional or multi-dimensional arrays as function parameters in C Language
Content:


1. Pass a one-dimensional array to the Function
Method 1: Add a parameter to describe the number of arrays.
Method 2: give a special value to the last element of the array.

2. Pass a multi-dimensional array to the Function
The C language cannot pass a common multi-dimensional array to the function, because we
The specific length of each dimension must be known to provide the correct unit for address calculation,
We cannot exchange such data between real parameters and form parameters. You must provide one on the left.
All dimensions outside the dimension

Method 1: Fun (int m [20] [30]). This enables the function to process only 20 rows and 30 columns of arrays.
Note: The first dimension of a multi-dimensional array does not need to be shown. We can provide sufficient information.

Method 2: Fun (int m [] [30]) or fun (INT (* m) [30])
There is no limit on the first dimension.

Method 3: Use the iliffer vector for fun (INT ** m) (for two-dimensional arrays)

Method 4: discard multi-dimensional arrays and provide its own subscript Method
For example, a two-dimensional array can be defined as a one-dimensional array char array [row_size * I + J];
3D or more arrays should be divided into several dimensions or fewer arrays.

As some methods have been mentioned in the previous program, this only lists method 1 and
Method 4.
========================================================== ========================= */
Int M [2] [3] = {0, 1, 2}, {3, 4, 5 }};

Fun1 (m); // the dimension of the form parameter and the real parameter group is exactly the same.

Int N [2*3] = {0, 1, 2, 3, 4, 5 };

Fun2 (n, 2, 3); // The same three-dimensional and multidimensional arrays can be decomposed as well.
}

Source code running result:

The first test value is: 1

The first test value is 4.

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.