Mail Archives: djgpp-workers/2000/06/20/12:00:13
Eli Zaretskii wrote:
> If you can run the trivial implementation via some test suite that proves
> that it indeed works, then I agree.
How about following?
It should print
10
20
24
And it does that with both trivial and builtin va_copy implementation.
What are additional cases which should be tested?
#include <stdarg.h>
#include <stdio.h>
/* At least two additional ints should be passed. */
/* The last arg should be zero. */
void magic_sum(int first, ...)
{
va_list args;
va_list arg_copy;
int i, sum = first;
va_start(args, first);
/* Sum up everything twice */
va_copy(arg_copy, args);
do
{
i = va_arg(args, int);
sum += i;
} while (i);
printf("Now %d\n", sum);
sum += first;
do
{
i = va_arg(arg_copy, int);
sum += i;
} while (i);
printf("Now %d\n", sum);
/* Add everything one more time except first three args. */
va_start(arg_copy, first);
va_arg(arg_copy, int);
va_arg(arg_copy, int);
va_copy(args, arg_copy);
do
{
i = va_arg(arg_copy, int);
sum += i;
} while (i);
printf("And finally %d\n", sum);
va_end(args);
va_end(arg_copy);
}
int main(void)
{
magic_sum(1, 2, 3, 4, 0);
return 0;
}
- Raw text -