[comp.lang.c] A questions on pointers, functions, and names

eckstein@grad1.cis.upenn.edu (Craig Eckstein) (03/21/91)

Two related questions:

First, is there a way to obtain the name of a function (as a character string)
given a pointer to the function.

Second, is there way to do the reverse.  Given the name of a function, is there
a way to obtain a pointer to it.

Please send all replies to eckstein@grad1.cis.upenn.edu





--Craig

*******************************************************************************

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (03/26/91)

In article <39499@netnews.upenn.edu>, eckstein@grad1.cis.upenn.edu (Craig Eckstein) writes:
> Two related questions:

> First, is there a way to obtain the name of a function (as a
> character string) given a pointer to the function.

> Second, is there way to do the reverse.  Given the name of a
> function, is there a way to obtain a pointer to it.

I assume you are not interested in answers involving things like

	struct fxntbl {
	  char *name;
	  void (*fxn)(); } fxns[] = { { "main", main },
				      { "fxn1", fxn1 },
				    };

In both cases, then, the answer is "no portable way, often no way".

Some environments do not keep this information that late; by the time
the program runs there may no longer be any record of the
correspondence between function names and pointers to them.  (UNIX is
this way if the executable has been stripped, for example.)

Thus, there is no hope for a portable way, because there exist
environments where it's impossible.

Those environments that keep the information around often do not
provide any reasonable way for the running program to get at it.  UNIX,
for example, generally does not make the symbol table available at
run-time; if you want it, you have to locate the executable and read it
yourself.  Even when this is possible (which generally involves making
unguaranteed assumptions about argv[0]), there is no way to ensure that
the executable file you find is the one you're running.  And even if
you ignore those issues, there is no guarantee you can read it (it may
have the execute bit but not the read bit turned on).

The reverse question is basically the same thing, just looking at the
database in reverse.  Since the desired database doesn't exist (or
rather, can't be counted on to exist or be accessible if it does), you
won't have much luck trying to access it :-)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu