3

forward argument list from a c++ class member function to a c function, (a wrapper I am creating)

not sure if it is correct? look in the comment inside argsPrinter

// c++ a class function

void argsPrinter( const char *format , ... ){
   //markFile(&mApiObj, format , ...); how to pass forward the ... to the c function

/*
va_list args;
va_start (args, format);
markFile(&mApiObj, format , args);
va_end(args);
*/
}

// c function

void markFile(someCustomApi* a, const char *format , ...)
{
   FILE *file= fopen(a->somePath, "a");
   if(file)
   {
     va_list args;
     va_start (args, format);
     vfprintf(file, format, args);
     va_end (args);
     fclose(file);
   }
//else do nothing
}

Edit the implementation changed, however, I may consider implementing an aditional function, if forward is not allowed to the ...

as markFile(&mApiObj, format , ...);

aah134
  • 860
  • 12
  • 25
  • 1
    [This][1] may help you. [1]: http://stackoverflow.com/questions/1719784/c-programming-forward-variable-argument-list Please try more search, before you post. – Smax Smaxović Sep 16 '13 at 15:43
  • possible duplicate of [How can one variable-args function call another?](http://stackoverflow.com/questions/1714550/how-can-one-variable-args-function-call-another) – Raymond Chen Sep 16 '13 at 15:52
  • possible duplicate of [C/C++: Passing variable number of arguments around](http://stackoverflow.com/questions/205529/c-c-passing-variable-number-of-arguments-around) – Kuba hasn't forgotten Monica Sep 16 '13 at 19:22

4 Answers4

3

You can't. If the library containing markFile doesn't provide a markFileV (similar to vsprintf being the counterpart to sprintf), it's just not possible to do this.

Given that you have the source of the C function, you may be able to change that, though.

On a side note (if you have any influence on the C code), this may be a reduced example, but why is markFile first formatting into a buffer (that is PATH_MAX chars long, of all things! What does that have to do with anything?) and then using fprintf to write out the results? Why not just use vfprintf directly?

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
2

It's not possible to forward C-style variadic arguments. That's why both fprintf and vfprintf exist. If you need to forward them, you'll have to create your own version of markFile, something like vmarkFile, which would accept a va_list instead of ....

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

How about just call your C++ function inside implementation of C function?

Such as your C++ function

void argsPrinter( const char *format , ... ){
   //markFile(&mApiObj, format , ...); how to pass forward the ... to the c function

   /*
   va_list args;
   va_start (args, format);
   markFile(&mApiObj, format , args);
   va_end(args);
   */
}

You can implement your C function by calling it:

void markFile(someCustomApi* a, const char *format , ...)
{
   a->argsPrinter(format, ... );
   //else do nothing
}
BigTailWolf
  • 1,028
  • 6
  • 17
0

Since it is highly ABI dependent you can not do this easily.

Alternatively you can use libffi. Look for variadic function call in the man page.

I still recommend that you write a function that accept a va_list as parameter like vsprintf and co.

mathk
  • 7,973
  • 6
  • 45
  • 74