kelvSYC said:
Using the value of a string to call a function with the same name. For
example, a way of calling a function named foo() using a string named
"foo". Then, if you change "foo" to "bar", the same code would call
bar().
Short answer: no.
Long answer: not directly. You'd have to implement a lookup table
that contains both the string and the function pointer. You'd search
the table for the string, then call the function using the associated
pointer.
Example:
/*
** Assuming all the functions you call return void, and
** take no arguments; simplest case
*/
typedef void (*fptr)(void);
/*
** Lookup table associating function name string
** with function pointer:
*/
struct flookup {
char fname[FNAMELEN];
fptr func;
};
void foo (void) {...}
void bar (void) {...}
struct flookup lkuptable[NFUNCS] = {"foo", foo, "bar", bar, ...};
void CallByName (char *fname)
{
int i = 0;
for (i = 0; i < NFUNCS; i++)
{
if (!strcmp (fname, lkuptable
.fname))
{
lkuptable.func ();
break;
}
}
}
Things get a bit trickier if you have to support functions with
different prototypes. You'd have to use a union or something to
handle the different function types, and you'd have to find a way to
pass the necessary arguments to the CallByName function. In all, the
bookkeeping gets to be more trouble than it's worth.