Xref: utzoo comp.lang.c:35873 comp.std.c:4254 Path: utzoo!attcan!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c,comp.std.c Subject: Re: (void *) to function pointer Message-ID: <1043@mwtech.UUCP> Date: 13 Jan 91 19:41:25 GMT References: <1991Jan9.000809.9152@brolga.cc.uq.oz.au> <1037@mwtech.UUCP> <1991Jan11.174923.7250@zoo.toronto.edu> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 43 In article <1991Jan11.174923.7250@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes: >In article <1037@mwtech.UUCP> martin@mwtech.UUCP (Martin Weitzel) writes: >>A less portable solution to transfer a function pointer to a `void *' >>is an intermediate cast to an `int'. The ANSI standard allows such >>casts... > >Yes and no. The wording of the standard implies, but does not quite >state explicitly, that there is *some size* of integer that is big enough >to hold a pointer. There is no promise that `int' suffices; it might >have to be `long', or even some nonstandardness like `long long'. OK, I thought I had allready put some warnings in my original article, maybe they weren't explicit enough: So let's state it this way: If you are concerned seriously about the portability of your code to any other combination than - the current hardware, - the current OS-version and - the current release of your compiler, you should FORGET about (void *) <-> (int or long) <-> (T (*)()) conversions. But there's another scenario which springs into my mind - it's the not so unusual `ANSI-C-has-broken-my-programs%'-complaint. It may well be the case that the brandnew compiler which supports the ANSI-C- specification for the first time will break programs which used `void *' or `char *' as universal pointers, even for function pointers. Of course this means that the hardware uses the same representation for all. A cast from `char *' or `void *' to a `T (*)()' with an intermediate integral type%% may shut up the complaints (of the compiler and the programmer :-)) and be in fact the fastest way to get things going again. ------ % : When my ears hear said complaints, my brains usually translate this into: "ANSI-C has shown me that my programs are not so portabel as I usually claim, but as I much depriciate to be reminded of that, please let me bash onto `brain-damaged-ANSI-C' and don't tell me of any changes to my programs which require more than a simple `global-search-and-replace'-command in the editor." %%: As Doug Gwyn pointed out, there is the chance of making a rather non-portable hack a bit more portable by using the largest integral type of the language, i.e. long. -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83