How does a VARARGS function call another VARARGS function?

Chris Torek chris at mimsy.umd.edu
Sun Jan 21 21:20:26 AEST 1990


[How about that, a question in comp.unix.wizards that belongs in
comp.lang.c, rather than the other way around....]

[No need to include text; the subject line suffices.  For those with
notes or other subject-truncating systems, it is:  How does a VARARGS
function call another VARARGS function?]

It does not.  There is no portable way for one function of variable
arguments to pass those arguments to another function of variable
arguments.  This is why vfprintf, vsprintf, and vprintf exist: a
function with variable arguments that wants to print those arguments
a la printf() must call vprintf.  vprintf is just like printf, except
that instead of a variable argument list, it takes a `va_list' parameter
that describes some other function's variable argument list:

/* foo(fmt, arg1, arg2) is like fprintf(stderr, fmt, arg1, arg2) */
int foo(const char *fmt, ...) {
	int ret, err;
	va_list ap;

	va_start(ap, fmt);
	ret = vfprintf(stderr, fmt, ap);
	if ((err = fflush(stderr)) != 0)
		ret = err;
	va_end(ap);
	return ret;
}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list