| delorie.com/archives/browse.cgi | search |
| From: | cwr AT cts DOT com (Will Rose) |
| Newsgroups: | comp.lang.c,comp.os.msdos.djgpp |
| Subject: | Re: Casting void pointers |
| Followup-To: | comp.lang.c,comp.os.msdos.djgpp |
| Date: | 23 Jun 1998 00:19:17 GMT |
| Organization: | CTS Network Services (CTSNET), San Diego, CA |
| Lines: | 33 |
| Message-ID: | <898561284.92944@optional.cts.com> |
| References: | <6mkaos$k7o AT dfw-ixnews6 DOT ix DOT netcom DOT com> <358DC1FA DOT 443E297D AT cs DOT com> <898511636snz AT genesis DOT demon DOT co DOT uk> |
| NNTP-Posting-Host: | optional.cts.com |
| Cache-Post-Path: | optional.cts.com!cwr AT crash-i2 DOT cts DOT com |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Lawrence Kirby (fred AT genesis DOT demon DOT co DOT uk) wrote:
: In article <358DC1FA DOT 443E297D AT cs DOT com> fighteer AT cs DOT com "John M. Aldrich" writes:
: ...
: >#include <stdio.h>
: >
: >int foo( int x )
: >{
: > return x + 10;
: >}
: >
: >int main( void )
: >{
: > void *vp = foo;
: Your compiler should have generated a diagnostic for this. The C language
: requires a cast to convert between void * and function pointers (in both
: directions). Note that C doesn't guarantee that a void * object can properly
: hold a funciton pointer. You should avoid doing this.
OTOH, I think:
int main(void)
{
void (*p)(void) = (void (*)(void)) &foo;
should work. I think the & operator is un-necessary on modern compilers.
Will
cwr AT crash DOT cts DOT com
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |