FA> Just curious why is REGS a union and not a struct ? It feels like one..
PE> Because they want to overlay the word-registers with byte-
PE> registers, instead of having them take up extra space. It
PE> doesn't feel like a normal structure, because instead of
PE> going regs.ax = 5, you have to go regs.x.ax = 5. BFN.
FA> But then how come that i can access both the wordregs and byteregs at
FA> the same time ?
Because they share the same space. I'm not sure what you don't understand here.
FA> Better yet, can you translate this into english ? :)
FA> With one exception, if a member of a union object is accessed
FA> after a value has been stored in a different member of the object,
FA> the behavior is implementation-defined./33/ One special guarantee is
FA> made in order to simplify the use of unions: If a union contains
FA> several structures that share a common initial sequence, and if the
FA> union object currently contains one of these structures, it is
FA> permitted to inspect the common initial part of any of them. Two
This is basically a different topic. Let's say you have a file containing
various record types, e.g. pensioner records and dole records.
struct dole_bludger {
int person_type;
char job_looking_for[50];
};
struct geriatric {
int person_type;
int number_of_grey_hairs;
};
union
{
struct dole_bludger db;
struct geriatric g;
} x;
x.db.person_type = 1; /* dole bludger = 1 */
if (x.g.person_type == 2) /* if a geriatric */
{
printf("I have %d grey hairs\n", x.g.number_of_grey_hairs);
}
That code is legal because the x.g.person_type and x.db.person_type are
guaranteed to refer to the same bit of storage and be all hunky dory etc.
BECAUSE the first element in BOTH structures is an integer. If the second
element in BOTH structures was a "long", the two are STILL
interchangeable. As soon as the data types diverge, all your guarantees
disappear. BFN. Paul.
@EOT:
---
* Origin: X (3:711/934.9)
|