void somefunc(...)

Started by
1 comment, last by krez 22 years, 3 months ago
i have seen functions with ellipses (sp?) as a parameter, someone said this is for a variable number of arguments. fair enough, what a nifty thing! but, how do you use those parameters in the function? of what datatype are they? do you even get a count of them? --- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Advertisement
They are any data type. You don't know how many there are by default, so you must indicate the stopping point on your own. Methods include passing a number of parameters, passing NULL as the last one (for a list of pointers that works well), passing a parse string (like *printf do), and probably some others . Here's an example of one that I wrote for libHFS (i.e. really simple). It accepts a list of char pointers (to strings) and stops when it hits NULL (which HFS_DIR_DONE is a macro for):
    #include <stdarg.h>int hfsChangeDirs(const char *Dir, ...) {  va_list ap;  char *Next;  if( chdir(Dir) != 0 ) return -1;  va_start(ap, Dir);    while((Next = va_arg(ap,char *)) != HFS_DIR_DONE) {      if( chdir(Next) != 0 ) {        va_end(ap);        return -1;      }    }  va_end(ap);  return 0;}  

You call va_start to begin the reading. You must pass the first parameter before the ellipse (there must be at least one parameter, of course). You need a va_list parameter for all of this. You pass the type of the data to va_arg to retreive the value of the next parameter. You must call va_end when you're done (as you can tell).

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on January 4, 2002 10:31:44 PM
your first argument somehow needs to communicate info about the ellipse args.

This topic is closed to new replies.

Advertisement