varargs -> varargs mystery

Ray Dunn ray at philmtl.philips.ca
Fri May 17 03:31:45 AEST 1991


In referenced article, gwyn at smoke.brl.mil (Doug Gwyn) writes:
 >In article <3110 at cirrusl.UUCP> Rahul Dhesi <dhesi at cirrus.COM> writes:
 >>Briefly, the question was:  if A and B are varags functions, how can
 >>one portably write them so that A uses one argument and passes the rest
 >>to B?  The short answer is:  you can't.
 >
 >Sure you can, but you have to pass the remaining arguments via a va_list
 >parameter, not scattered about.  I do this all the time.

What I've found this means in practice is that when you write a varargs
function it's usually best to write it as *two* functions, fn and vfn,
where fn creates the va_list and passes it vfn where the work is actually
done.

Both fn and vfn can then be called as required.

void
fn(int arg, ...)
{   va_list argsp;
      
    va_start(argsp, arg);
    vfn(arg, argsp);
    va_end(argsp);
}

void
vfn(int arg, va_list argsp)
{
    /* body of code */
}

Note that the example in the FAQ list defines a 'v' named function,
vstrcat, as taking a variable number of arguments not a va_list.

It's better style to keep to the implied convention that 'v' named
functions take a va_list.
-- 
Ray Dunn.                    | UUCP: ray at philmtl.philips.ca
Philips Electronics Ltd.     |       ..!{uunet|philapd|philabs}!philmtl!ray
600 Dr Frederik Philips Blvd | TEL : (514) 744-8987  (Phonemail)
St Laurent. Quebec.  H4M 2S9 | FAX : (514) 744-9550  TLX: 05-824090



More information about the Comp.lang.c mailing list