Sergei Organov writes:
 > Andrew Haley <[EMAIL PROTECTED]> writes:
 > 
 > > Sergei Organov writes:
 > >  > Ian Lance Taylor <[EMAIL PROTECTED]> writes:
 > >  > > Sergei Organov <[EMAIL PROTECTED]> writes:
 > >  > >
> >  > int float_as_int()
 > >  > {
 > >  >   h1.f = 1;
 > >  >   H0 h0 = *(H0*)&h1.f; // Should be OK? No, it is not?!
 > >
 > > I don't think this is OK.  Per C99, you can cast to the element type
 > > from the struct or vice versa, but you can't cast from one struct type
 > > to another via the first element.
 > 
 > There is no word "casting" in the definition of the aliasing rules in
 > C99. It talks about accessing of object through an lvalue of compatible
 > type. In this example I access stored value of object "h1.f"

No, that's not what the code above does.  You're accessing an object
of type h1 through an lvalue of type h0.  Accessing h1.f would have
been perfectly OK, but that's not what the code above does.

Look at it again:

   H0 h0 = *(H0*)&h1.f;

The LHS of this assignment is of type h0.  The RHS is an object of
effective type h1.  The fact that you're going via a pointer cast to
its first member is neither here nor there.

 > of type "float" through an lvalue of type "H0" that is an aggregate
 > type that includes "float" (a type compatible with the effective
 > type of the object) among its members. That IMHO should be OK from
 > C99 aliasing rules POV.

No.  You can only access an object through an lvalue of compatible
type.  In this case you're accessing an object of type h1 through an
lvalue of type h0.

 > > If this were possible, every struct type that started with an int
 > > would be in a single alias set.
 > 
 > How else should I interpret these C99 wordings (re-citing again):
 > 
 >   An object shall have its stored value accessed only by an lvalue
 >   expression that has one of the following types: {footnote 73}
 > 
 >      - a type compatible with the effective type of the object,
 >      [...]
 >      - an aggregate or union type that includes one of the aforementioned
 >        types among its members (including, recursively, a member of a
 >        subaggregate or contained union)

And this is neither.

Andrew.

Reply via email to