Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!uunet!sparky!rick From: rick@sparky.IMD.Sterling.COM (Richard Ohnemus) Newsgroups: comp.lang.c Subject: Re: varargs -> varargs mystery Message-ID: <1991May2.170148.12655@sparky.IMD.Sterling.COM> Date: 2 May 91 17:01:48 GMT References: <3099@cirrusl.UUCP> Organization: Sterling Software, IMD Lines: 78 In article <3099@cirrusl.UUCP> Rahul Dhesi 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 > >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 >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 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@sparky.IMD.Sterling.COM 1404 Ft. Crook Rd. South Phone: (402) 291-8300 Bellevue, NE. 68005-2969 FAX: (402) 291-4362