C Language Learning Tutorial fifth chapter-Functions (4)

Source: Internet
Author: User
Tags arrays final integer printf

Second, the array name as a function parameter

There are several differences between an array's masterpiece function parameter and an array element as an actual argument:
1. When you use an array element as an argument, the type of the array element that is 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. Parameter variables and argument variables are the two different memory units allocated by the compilation system when normal or subscript variables are used as function parameters. 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 be achieved? In the fourth chapter we have described that 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. Figure 5.1 illustrates this scenario. 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 in the same two
A deposit element (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 5.5] array A holds the results of a student's 5 courses, seeking average results.
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;
}
void Main ()
{
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);
}
Float aver (float a[5])
{ ......
}
void Main ()
{
......
for (i=0;i<5;i++)
scanf ("%f", &sco[i]);
Av=aver (SCO);
......
}
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. As discussed earlier, when a variable is used as a function parameter, the value transmitted 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. Example 5.3 confirms this conclusion. 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. To illustrate this, replace example 5.4 with the form of example 5.6. [Example 5.6] The topic is the same as in 5.4 cases. Use the array Masterpiece function argument instead.
void Nzp (int a[5])
{
int i;
printf ("\nvalues of Array A are:\n");
for (i=0;i<5;i++)
{
if (a[i]<0) a[i]=0;
printf ("%d", a[i]);
}
}
Main ()
{
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]);
}
void Nzp (int a[5])
{ ......
}
Main ()
{
int b[5],i;
......
NZP (b);
......
}
The function Nzp in this program is an integer group A with a length of 5. The real parameter Group B in the main function is also an integral type with a length of 5. In the main function, first enter the value of array b, and then output the initial value of array B. The NZP function is then called with the array name B as the argument. In Nzp, the negative unit is required to clear 0 and output the value of the parameter group A. After the main function is returned, the value of array B is output again. From the results of the operation, we can see that the initial and final values of array B are different, and the end value of array B and the group A are the same. This means that the argument parameters are the same array, and their values are changed at the same time. The following points should also be noted when using an array name as a function parameter:
A. The type of the parameter group and the real parameter group must be the same, or it will cause an error.
The length of the form parameter group and the real parameter group can be different, because when invoked, only the first address is routed and the length of the parameter group is not checked. When the length of the parameter group is inconsistent with the real parameter group, although there is no syntax error (compilation can pass), but the results of the program execution will be inconsistent with the actual, this should be noted. If the example 5.6 is amended as follows:
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]);
}
}
Main ()
{
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]);
}
This procedure and example 5.6 program ratio, NZP function of the shape parameter group length to 8, function body, for statement of the cyclic condition also change 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 element a[5],a[6],a[7 of array A is obviously meaningless. C. In the function parameter list, allow the length of the parameter group to be not given, or use a variable to represent the number of array elements.
For example: can be written 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.
From this, example 5.6 can be changed to the form of example 5.7.
[Example 5.7]
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]);
}
}
Main ()
{
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]);
}
void Nzp (int a[],int N)
{ ......
}
Main ()
{
......
NZP (b,5);
......
}
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.
D. Multidimensional arrays can also be parameters of 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])

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.