# Thread: arrays to va_list

1. ## arrays to va_list

This program works if I skip the first 3 arguments in the list but if I send "average(3, 0,1,2)" it works without skiping the first three arguments, as it should. Why can I not send the array?
Thanks for any help you can give me.

double average(int num,...)
{
va_list arguments;
double extra;
double numbers[25];

va_start(arguments,num);
double sum=0;
for(int x=0;x<num+3;x++)
{
if(x>2) //why do I have to do this to make this program work?
{
sum+=va_arg(arguments,double);
cout<<"\nso far "<<sum;
}
else
{
extra=va_arg(arguments,double);
}

}
va_end(arguments);
return sum/(double)num;
}

int main()
{
int q=25;
double mynum[25];

for(int x=0;x<=3;x++)
{
mynum[x]=(double)x;
}

cout<<endl<<"The Average is "<<average(x,mynum);
return 0;
}

2. Why do you want to use va_list with array? are you going to use many arrays in the call? like this: average(x, numarray1, numarray2, numarray3)? If not, skipp the va_list idea just have a function like this: average(int numInArray, int numArray[]). In fact I've never seen an array used as a va_arg. I think the point with va_list is to get rid of the array...

Someone else might have an better answer than this though...

3. Okay, so I'm an idiot. Thanks, I got caught up in trying to use the macros and forgot my basic skills!

I still wonder why it works skipping the first three arguments?

Popular pages Recent additions