Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Array name as function parameter in C language
Array name as function parameter in C language

To pass the array length as a parameter to the average function, you cannot calculate the array length through int arrLen = sizeof(a) / 4; inside the average function. Because float average(float a[10]) is equivalent to float average(float *a), sizeof(a) is always equal to 4, and sizeof(a) / 4 is always equal to 1.

It should be like this:

float average(float a[10],int arrLen) {

float sumSco = 0;

for (int i = 0; i < arrLen; i++)

{

sumSco += a[i];

}

return sumSco / arrLen;

}