C language calculates the length of the array (inside the function)

The length of the array can only be obtained where the array is defined! ! ! , cannot be obtained within the function!

#include <stdio.h>
#include <windows.h>

double avg(int *arr,int size){
    int count=0;
    for(int i=0;i<size;i++){
        count+=arr[i];
    };
    double avgVal=(double)count/size;
    return avgVal;
}
int main(){
    SetConsoleOutputCP(65001);
    int arr[]={1,2,3,4,5,6};
    int size=sizeof(arr)/sizeof(arr[0]);
    double avgVal=avg(arr,size);
    printf("%f",avgVal);
}

 

Guess you like

Origin blog.csdn.net/xuelang532777032/article/details/130060142
Recommended