root@MCIRPS2.MED.NYU.EDU (05/05/90)
I want to do somthing like the following in ''c'' I want to make an array of function calls, and assign the function call addresses into the array, and then pick the function to be executed by the value in i. The particular application is using pup functions. Depending on the context of the function call, I want to change the pup function address. The compiler does not let me do anything like this, but this is theme of what I would like. What am I doing wrong, or what should I do?? (This is meta code--- I know it does not work) ====== register i; long MyPup; void (*function)[i](); /* an array of function call address pointers */ void function1(); /* static function calls */ void function2(); void function3(); *function[0]=*function1(); /* put in function the transfer addresses of function[1]=*function2(); each of the static functions */ function[2]=*function3(); MyPup=defpup(); addtopup(MyPup,"What happens here ?? %f",function[i]); ======= -- +-----------------------------------------------------------------------------+ | karron@nyu.edu Dan Karron | | . . . . . . . . . . . . . . New York University Medical Center | | 560 First Avenue \ \ Pager <1> (212) 397 9330 | | New York, New York 10016 \**\ <2> 10896 <3> <your-number-here> | | (212) 340 5210 \**\__________________________________________ | +-----------------------------------------------------------------------------+
bennett@galois.sgi.com (Jim Bennett) (05/06/90)
karron@nyu.edu (Dan Karron) writes: > I want to make an array of function calls, and assign the > function call addresses into the array, and then pick the > function to be executed by the value in i. If I understand you correctly, you do it like this: ------------------------------------------------------------------------------- /* Define the functions. */ int func1 () {} int func2 () {} int func3 () {} /* functab is an array of pointers to functions. Notice that all of */ /* the functions must be the same type (type int in this case). */ int (*functab [3]) (); main () { int i; /* The following stores the addresses of the three functions in the */ /* table. Note that functions, like arrays, yield their address when */ /* referenced by name only. You could also build the table at compile */ /* time, which would be more efficient. */ (functab [0]) = func1; (functab [1]) = func2; (functab [2]) = func3; /* Then to call a function from the table, given an index, i, just do */ /* the following: */ (*functab [i]) (); } ------------------------------------------------------------------------------- Jim Bennett (bennett@esd.sgi.com)