Variable number of arguments

Stephen Clamage steve at taumet.com
Wed May 15 07:44:16 AEST 1991


ta-dw30 at cunixa.cc.columbia.edu (David Worenklein) writes:

>I know I can pull a variable number of arguments off of the stack using va_...
>but how do I know how many arguments are there? Is there any way to get the
>C compiler to push all the arguments and then push the number of (arguments,
>bytes, etc.) pushed?
>PS I'm using Symantec Think C v4 so if it has some feature that ANSI doesn't...

There is no portable way to do this; I don't know whether Think C
provides some extension.  One of the requirements of writing such a
function is that one of the fixed arguments must explicitly or
implicitly provide information about the number and type of the
arguments.  For example, the printf/scanf families interpret the
fixed string argument to determine what the remaining arguments are.

If you have a function which expects a variable number of args all of
the same type, it would be sufficient to have a fixed arg containing
the number of args.  Alternatively, if there is an "impossible" value
for the arg type (NULL pointer, a count of -1), you can use that
value as a flag for the end of the args.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list