bit fields in unions

Sorensen David sorensen at amdcad.UUCP
Mon Nov 11 08:29:07 AEST 1985


	I recently was writing a declaration for a LISP form which looks
something like this:

                    --------------------------------- 
		   |      14-bit integer             |
	---------------------------------------------
       | gc | tag1 |    either ^ or v                |
        ---------------------------------------------
		   | tag2 |   13-bit pointer         |
		    ---------------------------------

	The form is 16 bits wide, with tag1 selecting between the
14-bit integer or 13-bit pointer with another tag. Attempting to
write a C declaration for this:

struct form {
	unsigned int gc : 1;
	unsigned int tag1 : 1;
	union {
		unsigned int number : 14;
		struct {
			unsigned int tag2 : 1;
			unsigned int pointer : 13;
			} pval;
		} val;
	};

	resulted in a "bit-field outside of struct" error, where
number :14 was declared in the union.  I could not find any explicit
mention of bit-fields not being allowed in unions in K & R, but all
C compilers I have tried have not allowed them.  Does anyone know
of a C compiler that allows this?  Or does anyone know why this is not
allowed?

	Tim Olson
	Advanced Micro Devices



More information about the Comp.lang.c mailing list