[comp.std.c] Bitfields in unions

em@dce.ie (Eamonn McManus) (04/16/91)

Can a union contain a bitfield?  K&R2 obliquely implies no, while
gcc -pedantic says yes.  I'm inclined to believe K&R myself, though
at least on machines without alignment constraints such a thing
would seem reasonable.

,
Eamonn

lijewski@theory.tn.cornell.edu (Mike Lijewski) (04/19/91)

In article <carbide@dce.ie> em@dce.ie (Eamonn McManus) writes:

>Can a union contain a bitfield?  K&R2 obliquely implies no, while
>gcc -pedantic says yes.  I'm inclined to believe K&R myself, though
>at least on machines without alignment constraints such a thing
>would seem reasonable.

Certainly.  Quoting from 3.5.2.1, "A member of a structure or union may
have any object type.  In addition, a member may be declared to consist of
a specified number of bits (including a sign bit, if any).  Such a member
is called a bit-field;  its width is preceded by a colon."

>Eamonn


-- 
Mike Lijewski  (H)607/272-0238 (W)607/254-8686
Cornell National Supercomputer Facility
ARPA: mjlx@eagle.cnsf.cornell.edu  BITNET: mjlx@cornellf.bitnet
SMAIL:  25 Renwick Heights Road, Ithaca, NY  14850

gwyn@smoke.brl.mil (Doug Gwyn) (04/21/91)

In article <carbide@dce.ie> em@dce.ie (Eamonn McManus) writes:
>Can a union contain a bitfield?

Yes.

steve@taumet.com (Stephen Clamage) (04/21/91)

em@dce.ie (Eamonn McManus) writes:

>Can a union contain a bitfield?  K&R2 obliquely implies no, while
>gcc -pedantic says yes.  I'm inclined to believe K&R myself, though
>at least on machines without alignment constraints such a thing
>would seem reasonable.

The ANSI C standard says explicitly that a union may contain a bitfield.

The alignment argument is irrelevant, as it is up to the compiler to see
that an object of union type is aligned suitably.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

gvr@cs.brown.edu (George V. Reilly) (04/22/91)

In article <680@taumet.com> steve@taumet.com (Stephen Clamage) writes:
+ em@dce.ie (Eamonn McManus) writes:
+ 
+ >Can a union contain a bitfield?  K&R2 obliquely implies no, while
+ >gcc -pedantic says yes.  I'm inclined to believe K&R myself, though
+ >at least on machines without alignment constraints such a thing
+ >would seem reasonable.
+ 
+ The ANSI C standard says explicitly that a union may contain a bitfield.

Well, that's another thing that Harbison & Steele III have got wrong,
then.  On p. 128, it says:
	The syntax for defining components [in unions] is the same
	as that used for structures, except that bit fields are
	not permitted in unions.
________________
George V. Reilly   `Strike breaker'	gvr@cs.brown.edu   +1 (401) 863-7684
uunet!brunix!gvr   gvr@browncs.bitnet	Box 1910, Brown U, Prov, RI 02912

em@dce.ie (Eamonn McManus) (04/23/91)

gvr@cs.brown.edu (George V. Reilly) writes:
>+ The ANSI C standard says explicitly that a union may contain a bitfield.
>Well, that's another thing that Harbison & Steele III have got wrong,
>then.

As I mentioned in my original message, K&R2 also seems to imply
obliquely that unions may not contain bitfields.  Section A8.3:

    ...

    A struct-declaration-list is a sequence of declarations for the
    members of the structure or union:

    ...

    Usually, a struct-declarator is just a declarator for a member of a
    structure or union.  A structure member may also consist of a
    specified numbers of bits.  Such a member is also called a
    /bit-field/, or merely /field/; its length is set off from the
    declarator for the field name by a colon.

	struct-declarator:
	    declarator
	    declarator[opt] : constant-expression

Although the grammar allows union bitfields, the text by saying `A
*structure* member may also...' seems to be excluding unions.  It should
say `A structure or union member...' or just `Such a member...'.

,
Eamonn