home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!inmos!wraxall.inmos.co.uk!elberton!nathan
- Newsgroups: comp.lang.c
- From: nathan@inmos.co.uk (Nathan Sidwell)
- Subject: Re: Is that ansi ?
- Message-ID: <1993Jan8.165451.7873@wraxall.inmos.co.uk>
- X-Newsreader: TIN [version 1.1 PL8]
- References: <mcdonald.28@aries.scs.uiuc.edu>
- Date: Fri, 8 Jan 93 16:54:50 GMT
- Lines: 49
-
- J. D. McDonald (mcdonald@aries.scs.uiuc.edu) wrote:
- : In article <1993Jan8.142730.20697@osf.org> daniel@moonshine.osf.org (Daniel Dardailler) writes:
- : >A compiler is reporting the following warning:
- : > "void * and function pointers are not convertible to each other"
- : >when encountering:
- : > void * p = (void *) func ; /* func of any kind */
-
-
- : I'm afraid it's true, and one of the most serious faults of ANSI C.
-
- And a very good reason for it too. As I emailed to Daniel, some systems require
- more than an instruction pointer to reference a function. For example an
- environment pointer. Now ANSI could have said void * is big enough to
- hold any pointer, but then on such systems, most void pointers would carry
- extra unrequired baggage with them.
-
- : Function pointers are orthogonal to everything else. What you can do
- : is somehow convert them to an integer type and convert that to
- : void * pointer and then go back. The standard itself does not
- : guarantee that the conversion will get back the right answer however.
- : (For those who need a proof, checked with Doug Gwyn, that this can be done:
- : enclose the function pointer in a struct, write out the struct to a
- : file using fwrite, then read back in into a char array of the correct size.
- : This is guaranteed to work.) Another way is to try and use unions. This
- : is not absolutely guaranteed to work.
-
- : Doug McDonald
-
- Another way, which doesn't require file io is, (unchecked, I may
- have got the casting slightly wrong).
-
- void (*fptr)(void);
- char *cptr;
-
- fptr = (void (*)(void))function;
- cptr = (char *)&fptr;
-
- /* cptr now points to an array sizeof(fptr) of chars which are the function
- pointer */
-
- Now I'm not 100% sure that you can cast any function pointer to void (*)(void), and
- back again without loss, but I can find nothing in ANSI which states this, and
- can think of no reason why that might not be so.
-
- nathan
- --
-
- Nathan Sidwell INMOS UK | | nathan@inmos.co.uk DoD#0390
-
-