C - Passing function with unknown params to another function, and calling it -
i'm trying achieve this:
void sum(int a, int b){ printf("result: %d", a+b); } void callfunc(void (*funct)(...), ...) { va_list ars; va_start(ars, funct); funct(ars); va_end(ars); } int main() { callfunc(sum, 2,3); return 0; }
but doesn't work, because of needing of 2 va_list
s, funct
params , arguments passed. however, if try pass sum function, says:error: invalid conversion 'void (*)(int, int)' 'void (*)(...)'
so how make work old c-style?
you can't that. it's not possible.
the best can (while keeping generic) change funct
take va_list
, vprintf
. won't work purposes.
alternatively, can macro:
#include <stdio.h> #define call_func(func, ...) func(__va_args__) void sum(int a, int b){ printf("result: %d", a+b); } int main() { call_func(sum, 2, 3); return 0; }
Comments
Post a Comment