passing variable numbers of arguments

Andrew Koenig ark at alice.UUCP
Sun Jan 8 02:57:33 AEST 1989


In article <899 at thor.stolaf.edu>, mackenzi at agnes.uucp (David MacKenzie) writes:

> The following program produces unexpected results:

> #include <varargs.h>

> main ()
> {
>   printf ("foo:\n");
>   foo (1, 2, 3, 0);
>   printf ("bar:\n");
>   bar (4, 5, 6, 0);
> }

> foo (va_alist)
>   va_dcl
> {
>   bar (va_alist);
> }

> bar (va_alist)
>   va_dcl
> {
>   va_list list;
>   int i;

>   va_start (list);
>   while (i = va_arg (list, int))
>     printf ("%d\n", i);
>   va_end (list);
> }

There is no way to pass your entire argument list to another function.
The closest you can come is to pass a va_list.  For example:

main ()
{
  printf ("foo:\n");
  foo (1, 2, 3, 0);
}

foo (va_alist)
  va_dcl
{
  va_list list;
  va_start (list);
  bar (list);
  va_end (list);
}

bar (x)
  va_list x;
{
  int i;

  while (i = va_arg (x, int))
    printf ("%d\n", i);
}
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list