I have a problem with a design question in C.
Let's say that I have a fairly large number of functions with a different number of arguments.
POQ:
int print_one(int x)
{
printf("one: %d\n", x);
return 1;
}
int print_three(int x, int y, int z)
{
printf("three: %d-%d-%d\n", x, y, z);
return 3;
}
Now I want to associate some properties with these functions in the structure so that I can manipulate them without knowing the exact function, including their number of parameters (I could even name the interface of the structure)
I tried it like this: (& I think this is wrong):
typedef int (*pfunc)(int c, ...);
typedef struct _stroffunc
{
pfunc myfunction;
int flags;
int some_thing_count;
int arguments[10];
int argumentcount;
} stroffunc;
int main()
{
stroffunc firststruct;
firststruct.pfunc = (pfunc) print_two;
firststruct.something_count = 101;
arguments[0] = 102;
argumentcount = 1;
flag &= SOME_SEXY_FLAG;
firststruct.pfunc(firststruct.arguments[0]);
firststruct.pfunc(firststruct.arguments[0], 124, 11);
firststruct.pfunc(1, firststruct.arguments[0], 124, 1, 1);
}
I find this solution very ugly, and I think (hopefully) that there is a better solution for calling and setting function pointers.
I just hope I was clear enough ... NOTE. I did not compile this code, but I compiled and ran very similar, so the concepts work. NOTE: pure C required
source
share