About two-dimensional array parameter -------- [Badboy] in C language, two-dimensional array badboy

Source: Internet
Author: User

About two-dimensional array parameter -------- [Badboy] in C language, two-dimensional array badboy

Directly run the Code:

# Include

Void Fun (int * a [], int m, int n )//

{

Printf ("% d \ t", * a); // [0] [0]

/*

Int e [2] [2] [2] = {8, 7, 6, 5, 4, 3, 2, 1 };

Int * f, *** g;

G = e;

F = e; // There is a warning, but no error is reported

Printf ("% d \ n", * f );

*/

}

/* Explanation:

Int * a [2] in Fun () indicates that the pointer array a [2], a [0], a [1] stores all pointers,

A Indicates the first address of the array, so it is equivalent to a second-level pointer. (Explains why real arguments are second-level pointers)

Since a is a pointer, when a value is assigned, the pointer value of the real parameter is copied to a, so * a indicates a [0] [0];

The pointer level only indicates that the multi-level pointer is also a pointer. when passing a parameter, the system checks whether the pointer level matches.

However, the pointer stores all the address values. After the address value is changed, for example, if it points to the bucket that stores int-type data

The int type number is indicated by adding *.

You can assign values between pointers of different levels. (Explained * a = 4, but ** a is wrong )*/

Void Fun1 (int (* a) [], int m, int n) // int ** a will have a warning

{

Printf ("% d \ t", ** a); // int (* a) [2] can be fully operated using Arrays

// Int (* a) [] can only operate data with pointers

}

/* If it is int (* a) []; array pointer, pointing to the array pointer, second-level pointer;

The address of array a [], which is called a "Row Pointer "*/

Int main ()

{

Int a [3] [2] = {4, 5, 6, 1, 2, 3 };

Fun (int **) a, 3, 2 );//

Fun1 (a, 3, 2); // (int **)

Return 0;

}

Summary:

1. function parameter passing: the form parameter is a simple copy of the real Parameter

2. The length of the array (defined size) cannot be checked when passing parameters in the array)

3. Passing parameters in a two-dimensional array (a multi-dimensional array can be converted to a two-dimensional or one-dimensional array ):

1. Forced conversion to one-dimensional pointer, one-dimensional array

2. Pass the row pointer

3. Forcibly convert to a two-dimensional pointer (meaningless ). After passing parameters, you can only access them through pointer addressing. The array format is no longer applicable.

Therefore, if two-dimensional array parameters with uncertain rows and columns do not need to be converted into two-dimensional arrays. After passing parameters, it is also necessary to address them in the form of a one-dimensional array, so it is better to convert them into a one-dimensional array directly.

For a two-dimensional array with a specified number of columns, you can convert parameters into two-dimensional arrays.

For example, the parameter is defined as int a [] [6]. After passing the parameter, it can also be accessed in the form of an array like the original real parameter, which is very convenient.

4. When an array is passed as a non-reference type, the array is automatically converted to a pointer of the same type, that is, a copy of the corresponding type of real parameter is initialized.

When a function is called, the actual operation of the function is a copy of the pointer, instead of modifying the value of the real parameter pointer, but the value of the array element can be changed through the pointer. -- From Network

// My point of view: the reference transfer is also a simple copy, but it only adds a level-1 pointer and can be used to operate the target data through the pointer (it seems correct after verification)

Reference transmission: (from Baidu encyclopedia)

In C ++, function parameters are passed through references.

The reference transfer refers to the transfer of the actual parameter address to the function when the function is called. The modification made to the parameter in the function squadron will affect the actual parameter.

5. parameter transfer:

If you need to change the real parameter when passing a function or parameter that completes a certain function, it is best to operate and change the real parameter through reference transfer. It is best to return the execution status of the function to check the execution status of the called function and prompt it.



2-dimensional array Transfer Parameters in C Language

# Include <stdio. h> void gn1 (char a [], int length, char B []) {int I, k = 0; for (I = 0; I <6; I ++) {int j = 0x80; for (; j >>= 1) if (j & a [I]) B [k ++] = 1; elseb [k ++] = 0 ;}//; void gn2 (char B [], int length, char a [] [8]) // {int I, j, x = 0; for (I = 0; I <6; I ++) // {for (j = 0; j <8; j ++) // {a [I] [j] = B [x]; x ++ ;}}int main () {char a_1 [6], B _1 [48]; char a_2 [6] [8], I, j; // int I _1; for (I _1 = 0; I _1 <6; I _1 ++) scanf ("% c ,", & a_1 [I _1]); gn1 (a_1, 6, B _1); // you can obtain B _1 [48] and store 48 binary numbers ---- B _1 [48] gn2 (B _1, 48, a_2); for (I = 0; I <6; ++ I) {for (j = 0; j <8; ++ j) {printf ("% d", (int) a_2 [I] [j]);} printf ("\ n");} return 0 ;}

2-dimensional array parameters in C Language

Change the declaration, definition, and call of the double ave () function
Statement:
// Number of num array elements
Double ave (int * data, int num );
Definition:
Double ave (int * data, int num)
{
// Code omitted
}

Call:
Tmp [num] = ave (data [num], 4 );
// Note: 4 is the number of elements in each line of data [] []

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.