My problem is: I want to call the method, but I don't know at compile-time which arguments I want to pass.Is there some way by which I can create the va_list during run time and pass it to the fucntion.
Eg Code:
total(4, 1,2,3,4) ;//Where 4 is the number of arguments and 1,2,3,4 is my va_list;
int total( int numargs, ... )
{
int sum = 0;
int i ;
int arg;
va_list listpointer;
va_start(listpointer,numargs);
//printf(" The numarg value is %d\n",numargs);
while(arg!= 0)
{
arg = va_arg(listpointer,int);
//printf(" The arg returned %d",arg);
sum += arg;
}
va_end(listpointer);
printf("Total Purchase amount = %d\n", sum);
return sum;
}
the above works fine and gives me a correct output.
But, as you can see I am defining the va_list during compile time only but not in run time.Is there an elegant way to accept the arguments in runtime and pass it to the total function at runtime and it would add up and show the result.
Thanks
Sumit