David Rubin said:
Serve said:
Irrwahn Grausewitz said:
I'm suprised no one suggested a union!
#include <stdio.h>
union _x
{
long lng;
char byt[4];
Stop right here: what makes you think a long is four chars wide?
That could be easily overcome with sizeof. It's more that you can't rely on
the byte order if you try to do it portably.
No, the problem is that you can't write into one union field and read
from another.
With one exception, which is valid in this case:
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.70)
<<< look here below! >>>
One special guarantee is made in
order to simplify the use of unions: If a union contains
several structures that share a common initial sequence (see
below), and if the union object currently contains one of
these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of
the completed type of the union is visible. Two structures
share a common initial sequence if corresponding members
have compatible types (and, for bit-fields, the same widths)
for a sequence of one or more initial members.