This article mainly introduces how c ++ uses array names as function parameters to calculate array elements. For more information, see
The differences between using array names as function parameters and using array elements as real parameters are as follows:
(1) When array elements are used as real parameters, as long as the array type is the same as the type of the parameter variable of the function, the type of the array element used as the subscript variable is also the same as that of the function variable. Therefore, the form parameter of the function is not required to be a subscript variable. In other words, array elements are treated as normal variables. When using array names as function parameters, both the form parameters and the corresponding real parameters must be arrays of the same type and must have clear array descriptions. An error occurs when the parameters and real parameters are of different types.
(2) When common variables or subscript variables are used as function parameters, the parameters and real variables are two different memory units allocated by the compilation system. When a function is called, the value passing is to assign the value of the real variable to the shape variable. When using the array name as a function parameter, it does not pass the value, that is, it does not assign the value of each element of the real parameter array to each element of the form parameter array. Because the actual parameter array does not exist, the compilation system does not allocate memory for the parameter array. Because the array name is the first address of the array. Therefore, when using the array name as a function parameter, the transfer is actually the address transfer, that is, the first address of the real parameter array is assigned to the array name of the form parameter. After the array name of the form parameter obtains the first address, it is equal to the specific address. In fact, the form parameter array and the real parameter group are the same array, and a memory space is used together.
The Code is as follows:
// Example: one-dimensional array score, which stores the scores of 10 students and calculates the average value.
# 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 10 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 );
}
Note:
(1) using array names as function parameters should define arrays in the main and called functions.
(2) The real parameter array and the form parameter array should be of the same type. If they are inconsistent, an error will occur.
(3) In fact, specifying the size of the parameter array in the called function does not have any effect, because the C compiler does not check the size of the parameter array, only pass the first address of the parameter array to the parameter array.
(4) The size of the input parameter array can be left unspecified. When defining an array, the array name is followed by an empty brackets. to process the array elements in the called function, you can set another parameter, pass 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.2f \ n", average (score1, 5 ));
Printf ("the average of class B is % 6.2f \ n", average (score2, 10 ));
}
Note: For more exciting articles, please follow the help houseProgramming TutorialTopic.