Mail Archives: djgpp/2000/07/03/10:00:32
On Mon, 3 Jul 2000, Sean Proctor wrote:
o> okay, I posted this on comp.lang.os I don't think there is a way to
> do this well... what I'd basically like to do is have a pointer to a
> function, and a pointer to an argument list which I can put anything
> into... I'd like to call library functions, but I don't know which
> I'm going to call, or what the arguments are going to be at
> compilation. here's what I'd like to do:
>
> void *args;
> int (*general_fun)();
> int fun1(int);
> int fun2(float, int);
You may not pass different types/argument lists to a (*myfunc) () call.
You declared (*general_fun)() implicitely as (*general_fun)(void).
So you are allowed to call it without varlist only.
My first suggestion: If you want to pass only a limited type of different
var lists like here either int or float,int, pass it as structure:
typedef struct mystruct <brace> float foo; int bar; <brace>;
or (better) as pointer to mystruct: (*general_fun)(mystruct*)....
May be you want to have a union instead of a struct.
But in both cases you have to provide information how the varlist is to be
read:
e.g
---SNIP---
struct bla <brace> count_f; // number of passed floats
count_i; // number of passed ints
float *float_pointer //points to an array of floats
int *int_pointer // the same for ints
<brace>
---SNIP---
Alternatively, you can use the stdarg-lib which provides handling of
variable var lists.
Me, personally, I prefer the first method, because I can not help, the
stdarg-thingies look kinda weird and tricky ;-)
>
> int main(void)
> {
> int test, fun1_arg1, fun2_arg2;
> float fun2_arg1;
> ...
> if(test) {
> general_fun = fun1;
> args = fun1_arg1;
> }
> else {
> general_fun = fun2;
> args = malloc(sieof(float) + sizeof(int));
> *args = fun2_arg1;
> *(args + sizeof(float)) = fun2_arg2;
> /* I know the above 3 lines don't work at all, but I'm
> hoping someone will understand what I hope to accomplish by them and
> suggest a solution */
-----SNIP----
Uhoh, I think it's clear what <brace> means. My stone-aged MWM provides
some very weird xterm restrictions.
WBR
Stefan Wiermann
- Raw text -