C++ USES the array name as a function parameter to perform the corresponding operation on the array elements

  • 2020-04-02 02:23:01
  • OfStack

There are several differences between using array names as function arguments and array elements as arguments:

(1) when an array element is used as an argument, as long as the array type and the parameter type of the function are consistent, the type of the array element as the subscript variable is also consistent with the parameter type of the function. Therefore, it is not required that the formal parameter of the function be also a subscript variable. In other words, the array elements are treated as normal variables. When using the array name as the function parameter, it is required that the parameter and the corresponding argument must be the same type of array, and must have an explicit array description. An error occurs when a parameter and an argument are of different types.

(2) when ordinary variables or subscript variables are used as function parameters, formal variables and real variables are two different memory units allocated by the compilation system. When a function is called, the value is passed by assigning the value of a real argument to a parameter. When the array name is used as the function parameter, the value is not passed, that is, the value of each element of the real parameter group is not assigned to each element of the form parameter group. The compiler does not allocate memory for shape-parameter groups because they do not actually exist. Because the name of the array is the first address of the array. Therefore, when using the array name as the function parameter, we actually pass the address, that is, we assign the first address of the real parameter group to the shape parameter group name. When the first address is obtained from the shape parameter group name, the specific address is also obtained. In fact, the shape parameter group and the real parameter group are the same array, which jointly use a segment of memory space.


//Example: one-dimensional array score, which stores the scores of 10 students, and calculates the average.
#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;
}
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.2fn",aver);
}

Description:

(1) use the array name as the function parameter, should be in the main function and the called function respectively defined array.
(2) the type of the real parameter group and the shape parameter group should be consistent, if not, the result will be wrong.
(3) in fact, specifying the size of the shape parameter group in the called function has no effect, because the C compiler does not check the size of the shape parameter group, but simply passes the first address of the shape parameter group to the shape parameter group.
(4) shape parameter group can also not specify the size, when the array is defined in the array name followed by an empty bracket, in order to deal with the needs of array elements in the called function, can set another parameter, pass the number of array elements.


#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;
}
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));
}


Related articles: