Link to home
Start Free TrialLog in
Avatar of chowdry
chowdry

asked on

Variable Argument List

Hi experts,

I want to have a function with variable arguments.  my question whether the variable arguments will be stored automatically in va_list (or) I have to store it.  for example.

function declaration:

int calculate(int arg1, int arg2, va_list v1);

actual call:
------------
calculate(int arg1, int arg2, int arg3, int arg4);

the question is whether if I call calculate like above, whether the arguments arg3 & arg4 will be stored automatically in valist (or) I have to store it by myself before calling it.

Actually I don't to store the valist myself.  your sugesstions are welcomed.

Best Regards,
P R A T H A P
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexVirochovsky
AlexVirochovsky

I think, i must add some little example:
/* va_arg example */

#include <stdio.h>
#include <stdarg.h>

/* calculate sum of a 0 terminated list */
void sum(char *msg, ...)
{
   int total = 0;
   va_list ap;
   int arg;
   va_start(ap, msg);
   while ((arg = va_arg(ap,int)) != 0) {
      total += arg;
   }
   printf(msg, total);
   va_end(ap);
}

int main(void) {
   sum("The total of 1+2+3+4 is %d\n", 1,2,3,4,0);
   return 0;
}
Can be , it helps.
Alex