function passing of the array name as a parameter in C language

Source: Internet
Author: User

There are several differences between using array-famous function parameters and using array elements as arguments.

1) When an array element is used as an argument, the type of the array element that is the subscript variable is consistent with the type of the function parameter variable, as long as the type of the array is the same as the parameter variable of the function. Therefore, the function's formal parameters are not required to be subscript variables. In other words, the processing of an array element is treated as a normal variable. When using an array-famous function parameter, it is required that both the formal parameter and the corresponding argument must be an array of the same type, and must have an explicit array description. An error occurs when the parameters and arguments are inconsistent.

2) when a normal or subscript variable is used as a function parameter, the parameter variables and argument variables are two different memory units allocated by the compilation system. The value transfer that occurs when the function is called is to assign the value of the argument variable to the parameter variable. When using the array function parameter, it is not the transfer of the value, that is, the value of each element of the real parameter group is not assigned to each element of the parameter group. Since the actual parameter group does not exist, the compilation system does not allocate memory for the parameter group. So how does the data transfer work? As we've described, the array name is the first address of the array. Therefore, the transfer of the function parameters of the array is only the transfer of the address, that is, the first address of the real parameter group is given the shape parameter group name. Parameter group name after the first address is obtained, it is equal to having a real array. In fact, the shape parameter group and the real parameter group are the same array and have a memory space together.


illustrates this situation. A is a real parameter group with an integer type. A occupies a memory area with a 2000-headed address. b is the form parameter group name. When a function call occurs, the address is transmitted, the first address of the real parameter Group A is passed to the parameter group name B, and B is also taken to the address 2000. So a, b two array together occupy a 2000-headed address of a continuous internal deposit element. It is also possible to see that the same elements of A and B actually account for the same two memory units (two bytes per element of an integer array). For example A[0] and b[0] all Occupy 2000 and 2001 units, of course a[0] equals b[0]. The analogy is a[i] equals b[i].

"Example 1" in array a holds a student's score of 5 courses, seeking average results.

#include <stdio.h>floatAver (floata[5]){    inti; floatav,s=a[0];  for(i=1;i<5; i++) s=s+A[i]; AV=s/5; returnav;}intMainvoid){    floatsco[5],av; inti; printf ("\ninput 5 scores:\n");  for(i=0;i<5; i++) scanf ("%f",&Sco[i]); AV=Aver (SCO); printf ("average score is%5.2f", AV); return 0;}

This program first defines a real type function aver, a formal parameter is a real array, and the length is 5. In the function aver, the values of each element are added to the average and returned to the main function. The main function first completes the input of the array SCO, then calls the Aver function with the SCO as the argument, the function returns the value of AV, and the last output AV value. From the operating situation can be seen, the program has implemented the required functions.

3) As discussed earlier, the value transfer is unidirectional when a variable is used as a function parameter. That is, you can only pass arguments to a parameter, and you cannot return an argument from a formal parameter. The initial value of the formal parameter is the same as the argument, and when the value of the formal parameter changes, the argument does not change, and the final values are different. When using array function parameters, the situation is different. Because the actual parameters and arguments are the same array, the real parameter group changes when the shape parameter group changes. Of course this is not understood as the "bidirectional" value passed. However, from a practical point of view, the value of the real parameter group after calling the function will change depending on the value of the parameter group.

"Example 2"

#include <stdio.h>voidNzpinta[8]){    inti; printf ("\nvalues of array aare:\n");  for(i=0;i<8; i++){        if(a[i]<0) a[i]=0; printf ("%d", A[i]); }}intMainvoid){    intb[5],i; printf ("\ninput 5 numbers:\n");  for(i=0;i<5; i++) scanf ("%d",&B[i]); printf ("initial values of array B are:\n");  for(i=0;i<5; i++) printf ("%d", B[i]);    NZP (b); printf ("\nlast values of array b are:\n");  for(i=0;i<5; i++) printf ("%d", B[i]); return 0;}

This program Nzp function of the shape parameter group length to 8, the function body, the loop condition for the statement is also changed to I<8. Therefore, the length of the shape parameter Group A and the real parameter Group B is inconsistent. Compilation can pass, but from the result, the elements of array a a[5], a[6], a[7] are obviously meaningless.

③ in the function parameter list, the length of the shape parameter group is not given, or the number of array elements is represented by a variable. For example, it can be written as:
void Nzp (int a[])
or write as
void Nzp (int a[], int n)
Where the shape parameter group A does not give the length, and the N value dynamically represents the length of the array. The value of n is transmitted by the arguments of the keynote function.

"Example 3"

#include <stdio.h>voidNzpintA[],intN) {    inti; printf ("\nvalues of array a are:\n");  for(i=0; i<n;i++){        if(a[i]<0) a[i]=0; printf ("%d", A[i]); }}intMainvoid){    intb[5],i; printf ("\ninput 5 numbers:\n");  for(i=0;i<5; i++) scanf ("%d",&B[i]); printf ("initial values of array B are:\n");  for(i=0;i<5; i++) printf ("%d", B[i]); Nzp (b,5); printf ("\nlast values of array b are:\n");  for(i=0;i<5; i++) printf ("%d", B[i]); return 0;}

This program Nzp function parameter Group A does not give the length, determined by the n dynamic length. In the main function, the function call statement is NZP (b,5), where argument 5 assigns the parameter n as the length of the Shape argument group.

④ multidimensional arrays can also be used as arguments to functions. You can specify the length of each dimension when defining a function, or you can omit the length of the first dimension. Therefore, the following wording is legal:
int MA (int a[3][10])
Or
int MA (int a[][10]).

function passing of the array name as a parameter in C language

Related Article

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.