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
- From: "Moore, Mathew L" <MooreML at BATTELLE dot ORG>
- To: 'Claudio Bley' <bley at cs dot uni-magdeburg dot de>, Florian Weimer <fw at deneb dot enyo dot de>
- Cc: Joshua Nye <josh at boxcarmedia dot com>, Steve Dondley <s at dondley dot com>,gcc-help at gcc dot gnu dot org
- Date: Mon, 21 Oct 2002 09:06:52 -0400
- Subject: RE: Avoiding "assignment from incompatible pointer type" warning
Certainly it is legal as far as the standard is concerned,
"A pointer to an object or incomplete type may be converted to a pointer to
a different object or incomplete type."
However, it may not be safe,
"If the resulting pointer is not correctly aligned for the pointed-to type,
the behavior is undefined."
--Matt
> -----Original Message-----
> From: Claudio Bley [mailto:bley@cs.uni-magdeburg.de]
> Sent: Monday, October 21, 2002 07:22
> To: Florian Weimer
> Cc: Joshua Nye; Steve Dondley; gcc-help@gcc.gnu.org
> Subject: Re: Avoiding "assignment from incompatible pointer type"
> warning
>
>
> >>>>> "Florian" == Florian Weimer <fw@deneb.enyo.de> writes:
>
> Florian> "Joshua Nye" <josh@boxcarmedia.com> writes:
> >>> > int *p;
> >>> > float g = 3.141592653589793238;
> >>> > p = (int *)&g;
> >>>
> >>> Is this code legal? I doubt it.
> >>>
> >> 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? I mean, casting on the
> machine code level does just nothing - it's just "syntactic sugar" to
> convince the compiler to be quiet. I think casting from one pointer
> type to another does no harm at all. Am I wrong?
>
> --
> Claudio Bley ASCII ribbon campaign (")
> Debian GNU/Linux advocate - against HTML email X
> http://www.cs.uni-magdeburg.de/~bley/ & vCards / \
>