passing variable numbers of arguments

Andrew Koenig ark at alice.UUCP
Mon Jan 9 13:50:03 AEST 1989


In article <449 at marob.MASA.COM>, samperi at marob.MASA.COM (Dominick Samperi) writes:
> In article <15341 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:

> It seems that for the ANSI style varargs (that is, the one requiring
> stdarg.h), at least one explicitly named arg must be included in the
> function definition, so that there is a FIRST that can be supplied to
> va_start, as in:

> 	va_list ap ;
> 	va_start(sp, FIRST) ;

> 	x = va_arg(ap, TYPE) ;

> 	va_end(ap) ;

> Is this correct?

Yes.

>                  If not, how does one define a function with a variable
> number of args, for which there are no explicitly named args?

You can't.  The rationale is that there is no reason ever to do so.
After all, you have to know the type of the first argument somehow,
and without looking at the argument list.

> It also seems to be the case that it is not possible for a called function
> (with a variable number of args) to determine how many args were actually
> passed, or when the last arg has been fetched, unless this information is
> supplied in the first parameter, say.

Correct.

>                                        Wouldn't it have been reasonable for
> the standard to specify that the compiler should enable the called function
> to determine the number of parameters that were passed, by automatically
> passing this information as a first implicit parameter, for example?

Sure, but they didn't do it that way.

For one thing, that would slow down every call a little.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list