This is the code for finding the average of numbers passed as array in the function but it is giving incorrect length of the array when. It is known that array is converted into pointer but is there any way to find array length in the function(other than main)
#include <stdio.h>
int sum(int ar[]){
int avg=0;
int size= sizeof(ar) / sizeof(ar[0]);
printf("\nThe size of the array is: %d",size);
for(int i=0;i<size;i++){
avg+=ar[i];
// print("\n%d",avg);
}
return avg;
}
int main(int argc, char const *argv[])
{
int average;
int array[5]={1,2,3,4,5};
average=sum(array);
printf("\nThe average of the values in the array is: %d",average);
return 0;
}
The corresponding output that I am getting is: