Variable Parameters

Henry Spencer henry at utzoo.uucp
Sun Dec 31 12:37:46 AEST 1989


In article <1169 at zip.eecs.umich.edu> bagchi at dip.eecs.umich.edu (Ranjan Bagchi) writes:
>        I'm working, currently, on a project that involves a function that
>could take any number or arguments.  I would like to pass it only
>those arguments, i.e. no "flags" at the end which tell the function
>to stop.

Your function has to have some way of knowing how many arguments there
are, or which is the last argument.  The language does not provide any
built-in way for you to discover this.  The usual approaches are a
terminator argument of some kind, an argument count passed as the
first argument, or an implicit count embodied in an early argument
(e.g., you can tell how many arguments a printf() has by analyzing
the format string).  You have to do one of these things.

>        I'm using ANSI C, and believe my options are either to pass
>the function an array, or to use the elipsis for unspecified.

By far the simplest method is to pass an array with either a terminator
flag in the array or a count as a separate argument.  The ellipsis will
not solve the problem for you, and proper use of it is complex.
-- 
1972: Saturn V #15 flight-ready|     Henry Spencer at U of Toronto Zoology
1989: birds nesting in engines | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list