Article View: comp.sys.mac.programmer.codewarrior
Article #823Re: Function pointer casting
From: Seebs
Date: Mon, 02 Jun 2003 18:50
Date: Mon, 02 Jun 2003 18:50
29 lines
2081 bytes
2081 bytes
In article <3EDB6B20.2000804@lgl.kos.net>,
Edward Chew <ekc-noreply@lgl.kos.net> wrote:
>Interesting. I wonder why it would be illegal, and what the legal
>alternative would be?
Can't speak for C++, but in C:
* It's illegal because function pointers may be "bigger" than object pointers.
* The legal alternative is to pick an arbitrary "neutral" function pointer
type, and cast everything to and from that, because the language promises
that appropriately-cast function pointers are still magic.
So,
typedef void (*func_p)(void);
typedef int (*printf_t)(const restrict char *, ...);
func_p x = (func_p) printf;
int i;
i = *((printf_t) x)("hello, world!");
should work.
(I could have the casts wrong, I almost never need to do this.)
-s
--
Copyright 2003, all wrongs reversed. Peter Seebach / seebs@plethora.net
http://www.seebs.net/log/ - YA blog. http://www.seebs.net/ - homepage.
C/Unix wizard, pro-commerce radical, spam fighter. Boycott Spamazon!
Consulting, computers, web hosting, and shell access: http://www.plethora.net/
Technical Details
Message-ID:
<3edb9c71$0$1095$3c090ad1@news.plethora.net>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-out.visi.com!petbe.visi.com!gemini.plethora.net.POSTED!not-for-mailReferences:
<3ED6734A.6080600@lgl.kos.net> <mwron-5286CB.09413702062003@enews.newsguy.com> <3EDB6B20.2000804@lgl.kos.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10bcce,9c35d45628066e65 X-Google-Attributes: gid10bcce,public X-Google-Thread: f635e,9c35d45628066e65 X-Google-Attributes: gidf635e,public X-Google-ArrivalTime: 2003-06-02 11:50:26 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-out.visi.com!petbe.visi.com!gemini.plethora.net.POSTED!not-for-mail Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac Subject: Re: Function pointer casting References: <3ED6734A.6080600@lgl.kos.net> <mwron-5286CB.09413702062003@enews.newsguy.com> <3EDB6B20.2000804@lgl.kos.net> X-Newsreader: trn 4.0-test74 (May 26, 2000) From: seebs@plethora.net (Seebs) Date: 02 Jun 2003 18:50:25 GMT Lines: 29 Message-ID: <3edb9c71$0$1095$3c090ad1@news.plethora.net> NNTP-Posting-Host: 3487e213.news.plethora.net X-Trace: 1054579825 gemini.plethora.net 1095 205.166.146.8 X-Complaints-To: abuse@plethora.net