Question on function pointers

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Aug 23 14:53:46 AEST 1989


In article <9580003 at hpcuhb.HP.COM> swami at hpcuhb.HP.COM (V. Swaminathan) writes:
>I don't know how to cast the contents of a character pointer to function
>pointer

It's not a legal operation.  For one thing, functions and data objects may
reside in separate address spaces (as they did on some PDP-11 UNIX systems).

Your example, however, suffers from an even more fundamental problem.
You were trying to convert a character string "prtmsg" into a call to
the function denoted in the source code as prtmsg().  This cannot work
in general, because there are no source-code names left around at run
time, just executable binary bits.  There has recently been a related
discussion that mentioned interpreted environments, etc. wherein some
limited form of what you were attempting would be possible.  A more
universal solution is to have an array of structures, each of which has
two members, one the string form of the name and the other the
corresponding function pointer.  Then all you have to do is run through
the array until you find a matching name member, then invoke the function
via the other member of that array element.



More information about the Comp.lang.c mailing list