WAGREICH@BBNA.ARPA (09/07/84)
Could someone give me a small C example of how to declare (addresses of) functions in a jump table and how to use a jump table to call a function? I would prefer an actual example to a verbal explanation, or a pointer to a C program that uses this technique. Thanks.
gwyn@BRL-VLD.ARPA (09/07/84)
From: Doug Gwyn (VLD/VMB) <gwyn@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[] */
}