passing variable numbers of arguments

Kenneth Almquist ka at june.cs.washington.edu
Mon Jan 9 14:12:55 AEST 1989


Chris Torek gives some code for the situation in which one routine with
a variable number of arguments wants to have these arguments interpreted
by a different routine.  (See the end of this article.)  I've used the
same approach for lack of anything better, but is it portable?  The manual
page for varargs doesn't say anything about the effects of passing the
va_list variable to another routine.  The vprintf family of routines are
defined to accept va_list arguments, which should encourage implementors
to make this work, but doesn't force them to.  (They could have vprintf
perform some special operations to simulate the environment of its caller
before using va_arg.)  Perhaps the definition of the stdarg mechanism is
more specific?

Also, I'm curious about why Ansi C uses the stdarg mechanism instead of
varargs.  I would think that varargs would be easier to implement.  On
machines with simple calling conventions varargs requires no compiler
support; on other machines varargs is likely to require fewer special
cases than stdarg since either all or none of the arguments are handled
by the varargs mechanism.
					Kenneth Almquist




bar(va_alist)
	va_dcl
{
	va_list ap;

	va_start(ap);
	vbar(ap);
	va_end(ap);
}

vbar(ap)
	va_list ap;
{
	... code to deal with variable arguments ...
	... [uses va_arg(ap, type) to access arguments to "bar"] ...
}



More information about the Comp.lang.c mailing list