Question on function pointers

V. Swaminathan swami at hpcuhb.HP.COM
Wed Aug 23 04:58:29 AEST 1989


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);
}



More information about the Comp.lang.c mailing list