J
Johannes Schaub (litb)
Hello all. What makes these two codes different:
union A {
int a;
float b;
};
Let's assume int has no trap representations, and size of an int is that of
a float. Then, i read that we are allowed to do the following, and it would
not be undefined behavior:
union A a;
a.b = 3.1f;
int c = a.a;
But why is this? 6.5/7 says that we are not allowed to read the value of an
object having effective type "float" by an lvalue having type "int".
Contrary, if i do the following, some sources i read say that behavior is
undefined for the above reason. However what makes this case different?
int *pc = &a.a;
int c = *pc;
What am i missing? Is this reading from an lvalue of "an aggregate or union
type that includes one of the aforementioned types among its members"? I
can't think of a way this would be true, since the lvalue used in the above
is "a.a", and it definitely has not the type "union A", but the type "int" -
the same type that the lvalue "*pc" has - so why is the one undefined, and
the other not!?
Any hints, please? Thanks!
union A {
int a;
float b;
};
Let's assume int has no trap representations, and size of an int is that of
a float. Then, i read that we are allowed to do the following, and it would
not be undefined behavior:
union A a;
a.b = 3.1f;
int c = a.a;
But why is this? 6.5/7 says that we are not allowed to read the value of an
object having effective type "float" by an lvalue having type "int".
Contrary, if i do the following, some sources i read say that behavior is
undefined for the above reason. However what makes this case different?
int *pc = &a.a;
int c = *pc;
What am i missing? Is this reading from an lvalue of "an aggregate or union
type that includes one of the aforementioned types among its members"? I
can't think of a way this would be true, since the lvalue used in the above
is "a.a", and it definitely has not the type "union A", but the type "int" -
the same type that the lvalue "*pc" has - so why is the one undefined, and
the other not!?
Any hints, please? Thanks!