I want to implement a function that take a function and its arguments then call it. I have search a lot and could not implement it with variadic function. I want a clean way to record stdio after calling a function for unit test.
Even if you want to suggest another way to record stdio, please help me solve the original question.
something like this, but working.
void
foo(void (*function)(...), ...) {
va_list args;
va_start(args, function);
// This line is just to explain what i want
function(args);
va_end(args);
}
void
bar(int a, char *b) {
printf("a = %d, b = %s\n", a, b);
}
int
main() {
foo(bar, 2, "hello");
return 0;
}
the above code ....................
It does not work like you think. You need to pass the
va_listto the function.https://godbolt.org/z/db8nKPbrq
You can also use variadic macros (but of course it is done compile time):
https://godbolt.org/z/j16focM9o