Ben Bacarisse <> writes:
> Keith Thompson <kst-> writes:
>> pete b <> writes:
>>> Given this code:
>>> void f(void){}
>>> int main(void){return (int)f+5;}
>>>
>>> Is there anything wrong with this in terms of the standards? Is this
>>> legal C code?
> <snip>
>> The code violates no constraints as far as I can tell, so the
>> implementation is not required to diagnose any problems.
>>
>> Conversion from a pointer-to-function type to an integral type is
>> not forbidden, but nothing in the standard defines its behavior,
>> so the behavior is undefined by omission.
>
> Surely it is implementation defined (6.3.2.3 p6)?
>
> <snip>
Whoops, you're right. Conversion of a pointer-to-function to a
pointer-to-object, or vice versa, is undefined (except for null
pointers); that's what I was thinking of. Sorry about the
misinformation.
I just noticed something odd. C99 6.4.2.3p6 says:
Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If
the result cannot be represented in the integer type, the
behavior is undefined. The result need not be in the range of
values of any integer type.
I see no "previously specified" behavior for pointer-to-integer
conversions. (Conversion of a constant 0 to a pointer yields a null
pointer, but nothing is guaranteed for the reverse conversion;
(int)(void*)0) needn't yield 0.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"