[comp.std.c] Initialized bitfileds - help me read

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (03/14/89)

Are there any *standard* caveats about bitfield initialization? I have
read dpANS 3.5.2.1 (bitfields) and 3.5.7 (initialization) without
finding any, but I had one of my students bring me a simple program
which fails on two compilers, and I want to be sure that I'm not missing
something before I tell him that both compilers are broken.

I have already added this to my personal list of things to avoid in a
program I have to port to a number of machines...

Mail or post depending on your perception of public interest, if you can
point out a section I've missed, feel free.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

henry@utzoo.uucp (Henry Spencer) (03/16/89)

In article <13363@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>Are there any *standard* caveats about bitfield initialization? ...

One:  many old compilers won't do it, or don't do it correctly.
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

afscian@violet.waterloo.edu (Anthony Scian) (03/17/89)

In article <1989Mar15.165019.21562@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>In article <13363@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
>>Are there any *standard* caveats about bitfield initialization? ...
>One:  many old compilers won't do it, or don't do it correctly.
What does the standard say about this? Is there a correct ANSI standard
method? I don't recall seeing anything in the standard detailing this.

//// Anthony Scian afscian@violet.uwaterloo.ca afscian@violet.waterloo.edu ////
"I can't believe the news today, I can't close my eyes and make it go away" -U2

karl@haddock.ima.isc.com (Karl Heuer) (03/17/89)

In <1989Mar15.165019.21562@utzoo.uucp> utzoo!henry (Henry Spencer) writes:
>In <13363@steinmetz.ge.com> crdos1!davidsen (bill davidsen) writes:
>>Are there any *standard* caveats about bitfield initialization? ...
>
>One:  many old compilers won't do it, or don't do it correctly.

Since Bill posted to comp.std.c and referenced the (d)pANS, I presume that old
(in the sense of pre-ANSI) compilers are not the issue.  On the other hand,
since he didn't post some sample code and output, I'm also not sure what the
problem is.  The only such bitfield confusion I know of offhand concerns the
rules for structs that have unnamed bitfields.  Is that what we're talking
about?

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

henry@utzoo.uucp (Henry Spencer) (03/18/89)

In article <12417@watdragon.waterloo.edu> afscian@violet.waterloo.edu (Anthony Scian) writes:
>What does the standard say about this? Is there a correct ANSI standard
>method? I don't recall seeing anything in the standard detailing this.

Precisely.  Initialization of bitfields is not special in any way in
ANSI C -- they are ordinary structure members for this purpose.
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

karl@haddock.ima.isc.com (Karl Heuer) (03/22/89)

In article <1989Mar17.175208.1064@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>Initialization of bitfields is not special in any way in ANSI C -- they are
>ordinary structure members for this purpose.

Except that "All unnamed structure or union members are ignored during
initialization" (3.5.7), and I believe that only bitfields can be unnamed.

So, what is the correct way to initialize an object of type
	struct alpha { int a; struct beta { int :3; } b; int c; }
, where one of the members is a struct with no initializable members?  Does
the initializer {1,2} initialize a and c?  Or is {1,{},2} the proper (fully-
bracketed) syntax?

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

scjones@sdrc.UUCP (Larry Jones) (03/23/89)

In article <12112@haddock.ima.isc.com>, karl@haddock.ima.isc.com (Karl Heuer) writes:
> So, what is the correct way to initialize an object of type
> 	struct alpha { int a; struct beta { int :3; } b; int c; }
> , where one of the members is a struct with no initializable members?  Does
> the initializer {1,2} initialize a and c?  Or is {1,{},2} the proper (fully-
> bracketed) syntax?

The most recent draft adds a caveat to section 3.5.2.1 (Structure
and union specifiers) that "If the struct-declaration-list
contains no named members, the behavior is undefined."  Thus your
example structure is non-portable which makes the initialization
question moot.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@sdrc.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
"When all else fails, read the directions."