How to create a function to call a variable arg function? [duplicate]
Possible Duplicate:
C/C++: Passing variable number of arguments around
I need to put all functions calling into a C API into a separate source file unit. It is due to defines interfering with other code. So to keep the possibly interfering code separate.
Anyway, most functions are eg int function2(int) so I just implement in separate file like this:
int XXfunction1(int val) { return function(val) }
But I also have functions with variable开发者_StackOverflow arguments like this:
extern int function2(int, ...)
So how can I write my own function which calls that?
This doesn't seem to work:
int XXFunction2(int val, ...) {
return function2(val, ...);
}
How do I write the function?
You need a "v" version of your method that can take a variadic list.
int function2v(int val, va_list arg_list)
{
//Work directly with the va_list
}
//And to call it
int XXFunction2(int val, ...) {
int returnVal;
va_list list;
va_start(list, val);
returnVal = function2v(val, list);
va_end(list);
return returnVal;
}
Function2 must be declared before XXFunction2 for it to work. What you can do is to but the function signature on a .h file and import it before using, otherwise C can't find Function2 while compiling XXFunction2, leading to a compiling error.
精彩评论