Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!usc!srhqla!demott!kdq From: kdq@demott.COM (Kevin D. Quitt) Newsgroups: comp.lang.c Subject: Re: Initializing bit fields Message-ID: <343@demott.COM> Date: 29 Jun 90 00:15:53 GMT References: <1656@crabcake> Reply-To: kdq@demott.COM (Kevin D. Quitt) Organization: DeMott Electronics Co., Van Nuys CA Lines: 39 In article <1656@crabcake> zhu@cs.jhu.edu (Benjamin Zhu) writes: >>"All unnamed structure or union members are ignored during initialization." >>ANSI 3.5.7 (page 72, line 38) Thus the middle zero initializes >>field2. >> > > That is what I thought before. However, after I tried this out > with my gcc compiler, it turns out that at least gcc is not doing > what I expected. Here is an example. > >main(void) >{ > struct { > unsigned int field1 : 2; > unsigned int field2 : 2; /* unsigned int field2 :2, */ > unsigned int : 2; /* :2; */ > unsigned int fieldn : 2; > } mask = { 1, 1, 1 }; > > printf("%u %u %u\n", mask.field1, mask.field2, mask.fieldn); >} > > Guess what's the printout? > > 1 1 0 /* should be 1 1 1 according to ANSI */ What version are you running, and on what machine? gcc 1.37.1 on a Motorola Delta 3000 series (680x0) produces the correct results, as does the native compiler. (THAT was a surprise - of course, I had to move the struct out of the main function and remove the void parameter). -- _ Kevin D. Quitt demott!kdq kdq@demott.com DeMott Electronics Co. 14707 Keswick St. Van Nuys, CA 91405-1266 VOICE (818) 988-4975 FAX (818) 997-1190 MODEM (818) 997-4496 PEP last 96.37% of all statistics are made up.