Functions pointers and stuff

Mark H. Colburn colburn at lhasa.SRC.Honeywell.COM
Tue Apr 11 03:41:18 AEST 1989


In article <1715 at leah.Albany.Edu> rds95 at leah.Albany.Edu (Robert Seals) writes:
>Is there some sneaky way to execute the function when all I know is
>the name of it, i.e., a pointer to several letters which spell it's name.

There is a portable way to do this, given that you know the domain of
functions which can be called, but it is rather unwieldy.  If the
function that you are calling is one of the functions in your program,
then you can build a jump table that contains the name of all of the
functions in your program, along with the function's address.

For example, you could do somthing like:

void *malloc();
int   main();
int   foo();

struct func_table {
    char *func_name;
    void *(*func_addr)();
} func_table = {
    { "malloc", malloc },
    { "main", main },
    { "foo", foo },
};


The disadvantage of doing this is that return values are not handled
nicely and all of your functions which go into the table must be
declared before the table is built.  This works best if all of the
functions return the same thing, such as an int, or void, or whatever.

Building the table can be kind of a pain as well, however, it could be
automated with a few scripts.

Providing that all of your return values are handled correctly, or you
have different tables for each type of return value, I see no reason
why this should not be portable.

I don't know if that is what you are looking for, but it might help...
Mark H. Colburn           MN65-2300		colburn at SRC.Honeywell.COM
Systems Administration and Support
Honeywell Systems & Research Center



More information about the Comp.lang.c mailing list