Re: Use of unions
- From: "Douglas A. Gwyn" <DAGwyn@xxxxxxxx>
- Date: Thu, 30 Mar 2006 18:26:55 GMT
qqqmac@xxxxxxxxxxx wrote:
x = l;
should be x = r.l;
Is the code fragment above an example of correct use of a union?
It's not portable, and not a good example of anything.
If you want to transmit an integer value to another host
that might not have the same data format, then more care
is needed (e.g. XDR).
I have read books (1,2) that say the assignment to x is incorrect as the
component last used before this assignment is the character array, not
the long component.
If you use unsigned char (as DRT suggested in a previous response)
and the size of long is no more than 8 bytes, then if the input
bytes originally came from a valid long representation on the same
platform the reconstructed long value will be the same as the
original. The C standard specifically allows any data type to be
treated as an intermediate byte array of type unsigned char. That
is not to say that you often need to do that.
.
- References:
- Use of unions
- From: qqqmac
- Use of unions
- Prev by Date: Re: How much did the AS/400 influence the C standard?
- Next by Date: Re: References on C99
- Previous by thread: Re: Use of unions
- Next by thread: SIMD?
- Index(es):
Relevant Pages
|