(void *) to function pointer
Martin Weitzel
martin at mwtech.UUCP
Mon Jan 14 06:41:25 AEST 1991
In article <1991Jan11.174923.7250 at zoo.toronto.edu> henry at zoo.toronto.edu (Henry Spencer) writes:
>In article <1037 at mwtech.UUCP> martin at 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 at mwtech.UUCP, voice: 49-(0)6151-6 56 83
More information about the Comp.lang.c
mailing list