void somefunc(...)
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)
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):
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
#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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement