0

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");
}
user84999
  • 101
  • 1
  • sum_avg is a local variable. Local variables no longer exist once they go out of scope, such as when the function they are created in returns. If you return the address of one from a function, it is invalid and must not be used. You might try creating your array in the caller and passing a pointer to it, for the function to fill in. There is another way, but that is for after you're more comfortable with the basics. – Avi Berger Oct 13 '22 at 00:16
  • >This question already has an answer here:!!!!!!!!!!!!!! my question here is related to the variadic function,but the other question is about a simple function ,so they can't accepts the same solution – user84999 Oct 13 '22 at 00:35
  • If z is a `double *` then z[0] is a double and *z[0] is a syntax error. "error: invalid type argument of unary '*' (have 'double')" unary '*' only works with an argument of pointer type. – Avi Berger Oct 13 '22 at 00:44
  • so what is the solution – user84999 Oct 13 '22 at 00:49
  • and for the first error , you say sum_avg is a local variable,yeah and i did not use it in the main function ,in the main function i called to the function not to the local variable – user84999 Oct 13 '22 at 00:55
  • You are missing the point. You need to spend some time thinking about these things. Study, practice and improve your understanding. [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/631266) – Avi Berger Oct 13 '22 at 01:08
  • There is a "one question per question" rule at StackOverflow. If you ignore that rule, be prepared that your question might get closed as duplicate if one part is affected. BTW: Your problem is not related to variadic functions at all. – Gerhardh Oct 13 '22 at 06:18
  • @AviBerger a list of C++ books isn't really best source for learning C. – Gerhardh Oct 13 '22 at 06:20
  • @Gerhardh You're right. I didn't remember that this was a C question when I wrote that comment & messed up with it. That list is useless/wrong here. I'd delete that comment now, except for the first part since I was trying to point out that it seems from the OP's prior comment that they hadn't understood the responses(mine or the duplicate) and needed to sort them out better. – Avi Berger Oct 13 '22 at 06:39

0 Answers0