i tried to create a function that return an array of sum and average ,but iam stuck with a problem in the return
the problem is:
warning: function returns address of local variable [-Wreturn-local-addr]
return sum_avg;
what i understand is that my function returned the address not the value of sum_avg
,so i think about create a pointer variable z in the main function and put in it the address of sum_avg
,for access to it's value .
so i add this code double*z=summationandavg(3,4.0,5.0,6.0);
in the main function.but nothing change , i still stuck with the error and
also i have an other error :
error: invalid type argument of unary '*' (have 'double')
printf("%f , %f ",*z[0],*z[1]);
, it happened when i did this printf("%f , %f ",*z[0],*z[1]);
,and when i use z[] without * the last error hide ,i want to understand why this error happened,i think that for access to the value that stored on an address which is also stored on a pointer , i have to use *with the name of pointer!!so what is the mistake here?
my code:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<float.h>
#include<string.h>
#include<conio.h>
#include<stdarg.h>
double* summationandavg(int c ,...)
{
va_list lon;
va_start(lon,c);
int x=0;
double sum_avg[2]={0.0,0.0};
while(x<c){
sum_avg[0]+=va_arg(lon,double);
x++;
}
sum_avg[1]=sum_avg[0]/c;
va_end(lon);
return sum_avg;
}
int main()
{
double*z=summationandavg(3,4,5);
printf("%f , %f ",*z[0],*z[1]);
system("pause");
}