problems with the C parser

Chris Torek chris at umcp-cs.UUCP
Fri May 30 10:37:51 AEST 1986


In article <5072 at topaz.RUTGERS.EDU>, brisco at topaz.RUTGERS.EDU (T.p.),
wanting to translate a Pascal packed array [0..31] of boolean
writes:
>	... I [tried]:
>
>	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
>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!

Hrum.  Where in K&R did you find something that implies the above
is in any way correct?  Try instead chapter 6, page 138:

	Other restrictions to bear in mind: fields are unsigned; they
	may be stored only in int's (or, equivalently, unsigned's);
	THEY ARE NOT ARRAYS; [emphasis mine] they do not have addresses,
	so the & operator cannot be applied to them.

Arrays must be addressible; bit fields are not addressible; and
therefore there are no arrays of bit fields, nor are there bit
fields of arrays.

As for solving the original problem---obtaining the equivalent
of `packed array [0..31] of boolean'---you will probably have to
make do with macros, e.g.:

	#define	BIT(name, index) ((name) & (1 << (index)))
	#define BIS(name, index) ((name) |= 1 << (index))
	#define BIC(name, index) ((name) &= ~(1 << (index)))

You can, at this point, dispense with the union:

	typedef long newtype;

is probably the best way to go: it is even portable to 16-bitters.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list