2

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 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?

Community
  • 1
  • 1
Angus Comber
  • 9,316
  • 14
  • 59
  • 107

2 Answers2

4

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;
}
Joe
  • 56,979
  • 9
  • 128
  • 135
-1

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.

Lucas Famelli
  • 1,565
  • 2
  • 15
  • 23