Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!greg From: greg@utcsri.UUCP (Gregory Smith) Newsgroups: net.lang.c Subject: Re: problems with the C parser Message-ID: <2878@utcsri.UUCP> Date: Sat, 31-May-86 10:45:16 EDT Article-I.D.: utcsri.2878 Posted: Sat May 31 10:45:16 1986 Date-Received: Sat, 31-May-86 11:33:20 EDT References: <5072@topaz.RUTGERS.EDU> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 42 Keywords: bitfield,structure,'%&#@* Summary: In article <5072@topaz.RUTGERS.EDU> brisco@topaz.RUTGERS.EDU (T.p.) writes: > Which I had translated into C as: > > typedef union { > int intfield; /* change the record name - keyword */ > struct bool{unsigned x[32]:1}; > } newtype; > > > The C parser barfed all over this. I tried every combination 'illegal field type', which is what I get, is not an example of parser-vomit by any stretch of the imagination. You did forget the ';' after the 1 in the above, though - which might cause the first '}' to be lost - instant upchuck. And you didn't name the second member of the union - that's not a syntax error, though. >of unsigned x:1[32], ..... that I could think of. Out of frustration >I looked up the syntax of the declarations in K&R and found that the >initial guess had been correct! > Am I going crazy or is our C parser brain-damaged? Can anyone >else compile the above structure declaration? Well, K&R says that the kinds of things that can put in a bitfield is implementation-defined - thus `declarator : constant_expression'. I think it would be rare, though, to find anything but unsigned ints and ints to be implemented. If an array was allowed, it would be declared as `unsigned x[32]:32', since the width applies to the whole declarator. As it stands, I think you are out of luck. Write 'unsigned bits32' and then use shifts and masks to get at them: #define boolread( rval, bitnum ) ( ((rval)>>(bitnum)) &1) #define boolset( lval,bitnum ) ( (lval) |= 1<<(bitnum)) #define boolclr( lval,bitnum ) ( (lval) &= ~(1<<(bitnum)) #define boolasg( lval,bitnum, new )( new? boolset( lval,bitnum):boolclr(lval\ ,bitnum) -- "We demand rigidly defined areas of doubt and uncertainty!" - Vroomfondel ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg