[comp.lang.c] Question on function pointers

swami@hpcuhb.HP.COM (V. Swaminathan) (08/23/89)

When I was experimenting out with function pointers in C, I came up with
this  C code. I don't know how to cast the contents of a character pointer
to function pointer ? Can this be done at all ? Logically it doesn't look
possible for me. Can any netters enlighten on this ?

-----------------------------------------------------------------------
#include <stdio.h>

char *prtmsg();
char *(*fnptr[])() = {prtmsg, prtmsg, NULL};

main()
{
	char *(*fnptr1)();
	char *msg="prtmsg";
	int j;

	for(j=0; fnptr[j] != NULL; j++)
	    printf("%s\n",fnptr[j]()); /* This works fine, as expected */

	fnptr1=(char *(*)())msg;       /* How do I cast this to function ptr? */
	fnptr1();                      /* Of course, there it is.. 
					   our familiar core dump !! */
}

char *prtmsg()
{
	char *rtn;

	rtn="First prtmsg";
	return(rtn);
}

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/23/89)

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.

mcdonald@uxe.cso.uiuc.edu (08/25/89)

>When I was experimenting out with function pointers in C, I came up with
>this  C code. I don't know how to cast the contents of a character pointer
>to function pointer ? Can this be done at all ? Logically it doesn't look
>possible for me. Can any netters enlighten on this ?

Casting a data pointer to a function pointer directly is illegal.
CAsting it to an appropriately long integer type and then casting THAT
to a function pointer is a legal operation, but the result of trying
to DO anything with the resulting function pointer is , most unfortunately,
not defined by the standard. It is even possible on some architectures
that the mere cast to a function pointer would load an illegal value
in some register somewhere, causing a dump, playing Rogue, or shooting
a missle at Mexico city. 

On the other hand, on architectures where it makes sense, it usually works:
MS-DOS and VMS come to mind. What is is good for, however, is not what
your example tries. You would want to cast a data pointer to a code pointer
if you have, somehow, gotten some CODE into a DATA array. This would
have been by actually COMPILING it in place (i.e. your program is
a compiler like Turbo or Quick C), you have read it in from a disk
array, etc. I have used this trick in many programs to do really
nifty things. But it is obviously not portable.

Doug McDonald