Re^2: Variable Parameters

Chris Torek chris at mimsy.umd.edu
Tue Jan 2 16:21:15 AEST 1990


In article <3387 at bacon.ptolemy.arc.nasa.gov> raymond at ptolemy.arc.nasa.gov
(Eric A. Raymond) writes:
[nargs() is]
>Difficult?  Expensive?  Not really, since at compile time each caller
>knows how many arguments it is passed, right?  (Note: this is true
>even if the function (i.e., called via a ptr) is unknown at compile-time.)

Difficult: no.  Expensive: possibly, although varargs functions are
relatively rare, so if this is done only for varargs functions, I would
say `probably not'.  But:

>... all we need to do is:
>  - count the number of (variable) args in each vararg function call
>  - pass the number of these args as an extra parameter
>  - make some function, say nargs(void), return the value of this
>    otherwise unavailable parameter.

>... it's not much to ask when one considers the benefits.

The benefits.  Aye, there's the rub.

C does not have values.  C has *typed* values.  You cannot ask or answer
anything about a value without first knowing its type.  For instance,
what value does the bit pattern 0x4120 represent?  Surprise, the answer
is `2.5', because I meant `VAX single precision floating point'.  (If
you had guessed 2.5, I would have said I meant an integer: 16772.)

There *are* functions that can get away with assuming a type---for
instance, execl() (or spawnl()).  They are not all that common: cf.
printf and scanf, which need types as well.

Even solutions of the form `pass a string denoting number of arguments
and type' can fall down, because there are complicated types that are
hard to describe: for instance, unions containing structures, and vice
versa.
-- 
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