varargs -> varargs mystery

Richard Ohnemus rick at sparky.IMD.Sterling.COM
Fri May 3 03:01:48 AEST 1991


In article <3099 at cirrusl.UUCP> Rahul Dhesi <dhesi at cirrus.COM> writes:
>Suppose A is a varargs function, and it uses the first argument
>supplied, and passes on the rest to B, which is also a varargs
>function.  What is the right way to do it?  The Sun varargs manual
>entry is a little confusing to me.  It says:
>
>    "The argument list (or its remainder) can be passed to another
>     function using a pointer to a variable of type va_list--in which
>     case a call to va_arg in the subroutine advances the argument-list
>     pointer with respect to the caller as well."
>
>So should I be declaring a variable in A and passing its address to B?
>A does not know many arguments it is receiving and passing on to B.
>All that A knows is the type of the one argument that it itself uses.
>
>Sample code follows.
>
>#include <varargs.h>
>
>int A(va_alist)
>va_dcl
>{
>   int i;
>   va_list ap;
>
>   va_start(ap);
>   i = va_arg(ap, int);
>   ... use i locally, pass remaining args to B ...
>   B(&ap);         <<<<==== Is this what the Sun man page wants?
>   va_end(arg);
>}
>
>int B(va_alist)
>va_dcl
>{
>   ... B uses its variable argument list which it receives from A ...
>}
>--
>Rahul Dhesi <dhesi at cirrus.COM>
>UUCP:  oliveb!cirrusl!dhesi

Just pass the variable argument list pointer instead of the address of 
the pointer.

The following sample code works (Saber-C doesn't complain about and I 
haven't had any core dumps yet.).

#include <varargs.h>

int A(va_alist)
va_dcl
{
   int i;
   va_list ap;

   va_start(ap);
   i = va_arg(ap, int);
   ... use i locally, pass remaining args to B ...
   B(ap);
   va_end(arg);
}

int B(args)
va_list args;
{
   char *cp;

   cp = va_arg(args, char *);
   ... etc.
}

-- 
I never receive credit for anything I write! (I'm an Ohnemus. 8-)

Rick Ohnemus                 UUCP:     uunet!sparky!rick
Sterling Software IMD        INTERNET: rick at sparky.IMD.Sterling.COM
1404 Ft. Crook Rd. South     Phone:    (402) 291-8300 
Bellevue, NE. 68005-2969     FAX:      (402) 291-4362



More information about the Comp.lang.c mailing list