passing variable numbers of arguments
Chris Torek
chris at mimsy.UUCP
Sun Jan 8 07:16:53 AEST 1989
In article <899 at thor.stolaf.edu> mackenzi at agnes.uucp (David MacKenzie) writes:
>The following program produces unexpected results:
>foo (va_alist)
> va_dcl
>{
> bar (va_alist);
>}
This program is quite thoroughly illegal. `va_alist' is allowed to be
defined as a `magic token' that tells the compiler `I accept variable
argument lists'; this magic token may well not be a valid argument. If
you get lucky, the compiler will object to the call to bar().
>In other words, if I call bar () directly, it works, but if I call it via
>foo (), it breaks, in an implementation-dependant way, no less. Why?
Because indirect calls to bar() can never pass a variable argument list.
>Is there anyway to get this to work?
You cannot call bar() from foo() (except with a fixed argument list).
What you CAN do is call a common routine from both foo() and bar():
foo(va_alist)
va_dcl
{
va_list ap;
va_start(ap);
vbar(ap);
va_end(ap);
}
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 ...
}
This is why vprintf, vfprintf, and vsprintf exist at all.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list