Page 93 - 6437
P. 93
return avg;
}
Now, let us call the above function as follows:
#include <stdio.h>
/* function declaration */
double getAverage(int arr[], int size);
int main ()
{
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
/* pass pointer to the array as an argument */
avg = getAverage( balance, 5 ) ;
/* output the returned value */
printf( "Average value is: %f ", avg );
return 0;
}
When the above code is compiled together and executed, it produces the following result:
Average value is: 214.400000
As you can see, the length of the array doesn't matter as far as the function is concerned
because C performs no bounds checking for formal parameters.
Return Array from a Function
C programming does not allow to return an entire array as an argument to a function.
However, you can return a pointer to an array by specifying the array's name without an index.
96