Domanda

I've just discovered variadic functions in C and have defined one as a general notification typedef, that as well as a pointer to a text string can optionally have whatever arguments sent along with it- useful as a generic debug function for instance where I want all the output string manipulation in one place.

Since I want my C files to be as generic as possible I have static variables that contain pointers to possible callbacks in higher code, populated in an init call. Since the pointers may be null if higher code isn't interested, I'd normally have a local wrapper that only calls through the pointer if it's not null. But I'm having trouble figuring out how to forward this fuzzy thing represented by '...' and simply calling the function with '...' in the argument list gives a syntax error.

Is there any way to do this, or am I stuck with having a dummy local handler and having init set null callbacks to a pointer to that?

È stato utile?

Soluzione

You can't pass on the variadic arguments. You have to fetch them into a va_list and pass this to the inner function.

Take a look at this Question at the C FAQ. It defines a variadic error function that wants to forward to printf. This is just your use case.

In the same FAQs, it is generally recommended to have a version taking va_list for every (or most) variadic functions

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top