Avoiding "assignment from incompatible pointer type" warning

Moore, Mathew L MooreML@BATTELLE.ORG
Mon Oct 21 06:07:00 GMT 2002


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 / \
> 



More information about the Gcc-help mailing list