Example of calling functions via jump tables?

gwyn at BRL-VLD.ARPA gwyn at BRL-VLD.ARPA
Sat Sep 8 06:54:38 AEST 1984


From:      Doug Gwyn (VLD/VMB) <gwyn at BRL-VLD.ARPA>

/*
	example of calling variable function
*/

#include <string.h>

extern int	funca(), funcb();	/* possible functions to call */

static struct entry
	{
	char	*code_name;		/* if `name' matches this... */
	int	(*function)();		/* ...then call this function */
	}	table[] =
	{
	{	"A",	funca	},
	{	"B",	funcb	},
	{	0,	0	}
	};

int
dispatch( name, argument )
	register char	*name;		/* used to determine function */
	double		argument;	/* typical function argument */
	{
	register struct entry	*tp;	/* -> table[] entry */

	for ( tp = table; tp->code_name != (char *)0; ++tp )
		if ( strcmp( name, tp->code_name ) == 0 )
			return (*tp->function)( argument );

	return -1;			/* name not found in table[] */
	}



More information about the Comp.lang.c mailing list