This article mainly introduces C + + to the array name as a function of the parameters of the corresponding operation of the elements, need friends can refer to the
There are several differences between using an array name to make a function argument and using an array element as an argument:
(1) When using an array element 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 must have an explicit array description of the parameters and the corresponding arguments must all be of the same type. An error occurs when the type of the formal parameter and argument is inconsistent.
(2) When using ordinary variable or subscript variable as function parameter, the formal parameter variable and the real parametric are all two different memory units allocated by the compiling system. The value passing in a function call is to assign the value of the argument variable to the parameter variable. When using an array name as a function parameter, it is not a transfer of values, that is, not assigning the values of each element of a 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. Because the array name is the first address of the array. Therefore, the transfer of the function parameter with the array name is actually the transfer of the address, that is, assigning the first address of the real parameter group to the parameter group name. When the parameter group name gets the first address, it is equal to the specific address. In fact, the form parameter group and the real parameter group are the same array and use a section of memory space together.
The code is as follows:
Example: one dimensional array score, storing 10 students ' scores, averaging.
#include
#include
#include
#include
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;
}
void 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.2fn", 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.
The code is as follows:
#include
#include
#include
#include
Float average (float array[],int N)
{int i;
float aver,sum=array[0];
for (I=1 I
Sum=sum+array[i];
aver=sum/n;
return aver;
}
void 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.2fn", average (score1,5));
printf ("The average of class B is%6.2fn", average (score2,10));
}
Note : Please pay attention to the triple programming Tutorials section for more wonderful articles .