Forward an invocation of a variadic function in C

In C, is it possible to forward the invocation of a variadic function? As in,

int my_printf(char *fmt, ...) {
    fprintf(stderr, "Calling printf with fmt %s", fmt);
    return SOMEHOW_INVOKE_LIBC_PRINTF;
}

Forwarding the invocation in the manner above obviously isn’t strictly necessary in this case (since you could log invocations in other ways, or use vfprintf), but the codebase I’m working on requires the wrapper to do some actual work, and doesn’t have (and can’t have added) a helper function akin to vfprintf.

[Update: there seems to be some confusion based on the answers that have been supplied so far. To phrase the question another way: in general, can you wrap some arbitrary variadic function without modifying that function’s definition.]

12 Answers
12

Leave a Comment