Path: utzoo!attcan!uunet!dino!uxc.cso.uiuc.edu!uxc.cso.uiuc.edu!ux1.cso.uiuc.edu!uxe.cso.uiuc.edu!mcdonald From: mcdonald@uxe.cso.uiuc.edu Newsgroups: comp.lang.c Subject: Re: Question on function pointers Message-ID: <225800210@uxe.cso.uiuc.edu> Date: 24 Aug 89 23:22:39 GMT References: <9580003@hpcuhb.HP.COM> Lines: 26 Nf-ID: #R:hpcuhb.HP.COM:9580003:uxe.cso.uiuc.edu:225800210:000:1354 Nf-From: uxe.cso.uiuc.edu!mcdonald Aug 24 09:26:00 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 ? 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