This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Avoiding "assignment from incompatible pointer type" warning
>>>>> "Florian" == Florian Weimer <fw@deneb.enyo.de> writes:
Florian> "Claudio Bley" <bley@cs.uni-magdeburg.de> writes:
>> >> What do you consider legal and why wouldn't it be?
>>
Florian> Casting a pointer from float to int can result in
Florian> undefined behavior on some implementations.
>> I can't see why. Can you elaborate on that?
Florian> Well, it's all in the standard, although it's a bit
Florian> scattered.
>> I mean, casting on the machine code level does just nothing -
>> it's just "syntactic sugar" to convince the compiler to be
>> quiet.
Florian> It's not. In C, pointers are NOT machine addresses.
Florian> Pointers are an abstract concept, and often, something
Florian> which would work if they were machine addresses, fails
Florian> miserably. For example, even if two pointers compare
Florian> equal (because they point to the same object), it's
Florian> possible that an operation on one of the two is
Florian> undefined, while it is defined for the other one.
>> I think casting from one pointer type to another does no harm
>> at all. Am I wrong?
Florian> Yes, you are, I'm afraid.
I'm afraid too! ;-) Really. I'm using C for quite a while now, read a
few books but I never realized that. I was thinking that a cast just
meant "pretend these bits have a different type, and treat them
accordingly". Also, as Mathew Moore mentioned, I was not thinking
about alignment issues.
Regards.
--
Claudio Bley ASCII ribbon campaign (")
Debian GNU/Linux advocate - against HTML email X
http://www.cs.uni-magdeburg.de/~bley/ & vCards / \