Re: FAQ-Question
- From: "Robert Gamble" <rgamble99@xxxxxxxxx>
- Date: 20 Jun 2006 12:13:58 -0700
Dann Corbit wrote:
"Till Crueger" <TillFC@xxxxxxx> wrote in message
news:pan.2006.06.20.18.53.22.108909@xxxxxxxxxx
Hi,
I stumbled upon the following code to determine byte ordering in the FAQ:
union {
int i;
char c[sizeof(int)];
} x;
/* do stuff */
In this case it seems that the union was used to convert a memory section
from int to char[]. However I've learned that you couldn't use unions to
reinterpret a memory area, because you can't always be sure that the
fields realy do use the same positions. So why is this code portable?
From : ISO/IEC 9899:1999 (E) (which is the current standard) we have this:
5 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 complete 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.
That does not have any bearing on the presented example though as there
are no structures involved.
Robert Gamble
.
- Follow-Ups:
- Re: FAQ-Question
- From: Dann Corbit
- Re: FAQ-Question
- References:
- FAQ-Question
- From: Till Crueger
- Re: FAQ-Question
- From: Dann Corbit
- FAQ-Question
- Prev by Date: I need C complier.
- Next by Date: Re: Educated guesses for efficiency?
- Previous by thread: Re: FAQ-Question
- Next by thread: Re: FAQ-Question
- Index(es):
Relevant Pages
|