Changing function transfer addresses

Jim Bennett bennett at galois.sgi.com
Sun May 6 14:59:49 AEST 1990


karron at 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 at esd.sgi.com)



More information about the Comp.sys.sgi mailing list