Thread: arrays to va_list

  1. #1
    Unregistered
    Guest

    Question 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. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    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. #3
    Unregistered
    Guest
    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 subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM