0

Old client code:

printf("foo: %d, bar: %s.\n", f, b);

What I'd like to replace that printf (& 100's others like it) with:

my_printf(ctrl1, ctrl2, "foo: %d, bar: %s.\n", f, b);

Implementation of my_printf

void my_printf(CtrlT1 c1, Ctrl c2, char* fmt, ...) {
/* Do stuff with c1 & c2 */
   fprintf(log, fmt, **WHAT_GOES_HERE**);
}

What I've tried so far

It seems like there ought to be a simple, direct way to pass the list of arguments associated with ... through to fprintf. I tried fprintf(log, fmt, ...); but the compiler complains, "syntax error before '...' token".

I also tried: va_list ap; va_start(ap, fmt); fprintf(log, fmt, ap);

The call with the va_list compiles and even runs without coring, but what's being passed to printf is plainly not the same thing as what was passed into my function as ..., as may be judged by the output (representation of non-printing char).

If push comes to shove, I could probably walk through the contents of the va_list by brute-force but that seems stupid. Is there no simple token or syntax to pass that ... through?

1 Answers1

5

This isn't a general solution, but for the stdio functions, look at the ones that start with the letter v, such as vfprintf. They take a va_list as their last parameter, instead of the ....

void my_printf(CtrlT1 c1, Ctrl c2, char* fmt, ...) {
    /* Do stuff with c1 & c2 */
    va_list ap;
    va_start (ap, fmt);
    vfprintf (log, fmt, ap);
    va_end (ap);
}
David Yaw
  • 27,383
  • 4
  • 60
  • 93