In C language, array names are used as parameters to implement function transfer

Source: Internet
Author: User

There are several differences between the function parameters of an array masterpiece and the argument of an array element.

1 when an array element is used as an argument, the type of the array element as the subscript variable is also the same as the type of the function parameter variable, as long as the type of the array and the types of the function's parameter variables are the same. Therefore, the parameter of a function is not required to be a subscript variable. In other words, the processing of an array element is treated as a normal variable. When you use the array Masterpiece function argument, you require 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 formal parameters and arguments are inconsistent.

2 The parameter variable and the argument variable are two different memory units allocated by the compiling system when the normal variable or subscript variable is used as the function argument. The value transfer that occurs when a function is called is to assign the value of the argument variable to the parameter variable. When using an array masterpiece function parameter, it is not the transfer of the value, that is, not assigning the values of each element of the real parameter group to each element of the parameter group. Because the actual parameter group does not exist, the compilation system does not allocate memory for the shape parameter group. So how does the data transfer work? As we have described, the array name is the first address of the array. Therefore, the transfer of the function parameter of the array masterpiece is only the transfer of the address, that is, the first address of the real parameter group is given the parameter group name. After the first address is obtained from the parameter group name, it is also equivalent to having a real array. In fact, the form parameter group and the real parameter group are the same array and share a memory space.


The above illustration illustrates this situation. In the diagram, set a as the real parameter group, and the type is integral type. A occupies a block of memory with a 2000-headed address. b is the shape parameter group name. When a function call occurs, the address is transmitted, the first address of the real parameter Group A is transmitted to the form parameter group name B, and B also obtains the address 2000. So the a,b two arrays share a continuous internal deposit of 2000-headed address. You can also see from the diagram that the same elements of A and B are actually the same two memory units (an integer array of two bytes per element). For example A[0] and B[0] both Occupy 2000 and 2001 units, of course a[0] equals b[0]. The analogy has a[i] equals b[i].

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

#include <stdio.h>

Float aver (float a[5]) {
int i;
float av,s=a[0];
for (i=1;i<5;i++)
S=s+a[i];
AV=S/5;
return AV;
}

int main (void) {
float Sco[5],av;
int i;
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, there is a formal parameter is a real array of a, the length of 5. In the function aver, the values of each element are added to the average value and returned to the main function. In main function main, the input of SCO is finished first, then the Aver function is called by SCO as the argument, and the function return value is sent AV, and the AV value is finally output. From the operating situation can be seen that the program to achieve the required functionality.

3 It has been discussed before that when variables are used as function parameters, the value transfer is one-way. That is, only arguments can be passed from the argument, and the arguments cannot be returned from the formal parameters. The initial value of the formal parameter is the same as that of the argument, and when the value of the formal parameter changes, the argument does not change and the final values are different. And when you use the array masterpiece function parameters, the situation is different. Because the actual parameters and arguments are the same array, the real parameter group changes as the parameter group changes. Of course, this situation cannot be understood as a "two-way" value delivery occurred. However, from the practical point of view, the value of the real parameter group will change with the change of the parameter group value after calling the function.

"Example 2"

#include <stdio.h>

void Nzp (int a[8]) {
int i;
printf ("\nvalues of Array aare:\n");
for (i=0;i<8;i++) {
if (a[i]<0) a[i]=0;
printf ("%d", a[i]);
}
}

int main (void) {
int b[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 procedure Nzp function's shape parameter group length to 8, in function body, for statement's cyclic condition also changes to i<8. Therefore, the length of the form parameter Group A and the real parameter Group B are 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 parameter group is not given, or a variable is used to represent the number of elements of the array. For example, you can write as:

void Nzp (int a[])

or write as

void Nzp (int a[], int n)

The shape parameter group A does not give a length, but the length of the array is dynamically represented by the N value. The value of n is transmitted by the argument of the keynote function.

"Example 3"

#include <stdio.h>

void Nzp (int a[],int n) {
int i;
printf ("\nvalues of Array A are:\n");
for (i=0;i<n;i++) {
if (a[i]<0) a[i]=0;
printf ("%d", a[i]);
}
}

int main (void) {
int b[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-form parameter group A does not give the length, the length is determined dynamically by N. In the main function, the function call statement is NZP (b,5), where argument 5 assigns the parameter n as the length of the parameter group.

A ④ multidimensional array can also be used as a parameter to a function. A parameter group can specify the length of each dimension when the function is defined, or it 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]).


In fact, the form parameter group and the real parameter group are the same array and use a section of memory space together.

Starting address 2000


Set A to the real parameter group and type to reshape. A occupies a block of memory with a 2000-headed address. B is a parameter group. When a function call is made, the address is passed, and the first address of the real parameter Group A is transmitted to the form parameter group name B, and B also obtains the address 2000. The array A and B two collectively occupy a continuous deposit of 2000-headed address. At the same time, the same elements in A and b actually share the same memory unit (two bytes per element of the reshaping array).

Example 1: There is a one-dimensional array of score, storing 10 students ' scores, averaging.

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
Float average (float array[10]) {
int i;
float aver,sum=array[0];
for (i=1;i<10;i++)
Sum=sum+array[i];
AVER=SUM/10;
return aver;
}
Main () {
float Score[10],aver;
int i;
printf ("Input score:/n");
for (i=0;i<10;i++)
scanf ("%f", &score[i]);
printf ("n");
Aver=average (score);
printf ("Average score is%5.2f/n", aver);
}

Description

(1) using the function parameters of the array masterpiece, we should define the array separately in the keynote function and the modulated function.

(2) The real parameter group and the form parameter group type should be consistent, if inconsistent, the result will be wrong.

(3) In fact, specifying the size of the parameter group in the modulated function has no effect, because the C compiler does not check the size of the parameter group, but simply passes the first address of the parameter group to the form parameter group.

(4) Parameter groups can also not specify the size, when defining the array after the array name followed by an empty bracket, in order to handle the needs of the array elements in the called function, you can set another parameter, passing the number of array elements.

Example 2: Passing the number of array elements with parameters.

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
Float average (float array[],int N) {
int i;
float aver,sum=array[0];
for (i=1;i<n;i++)
Sum=sum+array[i];
aver=sum/n;
return aver;
}
Main () {
Float score1[5]={98.5,97,91.5,60,55};
Float score2[10]={67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5};
printf ("The average of class A is%6.2f/n", average (score1,5));
printf ("The average of class B is%6.2f/n", average (score2,10));
}

Multidimensional array as a function parameter

When the elements in a multidimensional array do function parameters, the function arguments are the same as the one-dimensional array elements, and the parameters of the multidimensional array name function are discussed here. Take a two-dimensional array for example:
When a two-dimensional array name is a function parameter, the formal parameter syntax is:
Type descriptor parameter name [] [constant expression m]
A parameter group can omit the length of one dimension.
For example: int array[][10]
Because the argument represents the array name, address delivery, the two-dimensional array is stored in memory by row precedence, does not really area branches and columns, in the formal parameters, you must indicate the number of columns in order to ensure that the real parameter group and the parameter group of data one by one corresponds, therefore, the length of the second dimension in the form parameter group can not be omitted.
When calling a function, the real parameter group corresponding to the parameter group must also be a two-dimensional array, and its second dimension must be equal to the length of the second dimension of the form parameter group.

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.