Need help with union trick: summary of replies

Eychaner, Glenn C. gceych at juliet.caltech.edu
Sun Jan 13 05:51:15 AEST 1991


dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes...
>I sort of like calling the union substructure by tiny names like x, y,
>z etc.  Then references become struct_name.x.field_name, and the ".x."
>is almost not noticeable.  The idea is to use a small meaningless name
>(sort of using i, j, k as loop counters) so the meaningfullness (to
>coin a noun) lies entirely in the name of the structure and the name of
>the field.
>--
Me (in VaxC, I don't know if others support it) use variant_structures and
variant_unions. Example:

struct {
    variant_union {
	unsigned char ch;
	variant_struct {
	    unsigned one : 1;
	    unsigned two : 1;
	    ...
	    unsigned eig : 1;
	} superfluous;
    } superfluous2;
    int i, j, k, x, y, z;
} mystructure;

So you can reference the first bit of ch as mystructure.one, the second as
mystructure.two, ch as mystructure.ch, i as mystructure.i, etc.  All the
variables must have different names, BTW.  I use this for storing bit flags
to a file as a char or int (to avoid huge scanf statements and files full
of 1's & 0's).

Glenn C. Eychaner | "We demand rigidly defined areas of doubt and uncertainty."
40386 N. Shore Ln `-----.                                             -D. Adams
Big Bear City, CA 92314 `-----------------------. !**** G O  N I N E R S ****!
Eychaner%SunCub.Caltech.edu at Xhmeia.Caltech.edu -|- Big Bear Solar Observatory



More information about the Comp.lang.c mailing list