Path: utzoo!attcan!uunet!ginosko!rex!uflorida!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: Question on function pointers Message-ID: <10801@smoke.BRL.MIL> Date: 23 Aug 89 04:53:46 GMT References: <9580003@hpcuhb.HP.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 19 In article <9580003@hpcuhb.HP.COM> swami@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.