When I try compiling this, I get the error message, "error: bit-field `<anonymous>' has invalid type"

Can bit-fields only be used on certain types? I thought that if I defined a large-enough variable, the massive bitfield needed for my application could then be defined because the bitfield has to be no larger than the type used to define it.

4 Answers
4

Bit fields must fit within a single int, you can't use arbitrary sizes. Honestly the ANSI bitfield implementation is kinda broken. It misses a lot of other stuff too, like control over padding and layout that real-world applications usually need. I'd consider writing some macros or accessor functions to abstract the larger sizes and giving up on the bitfield syntax.

Thanks, Andy. That's kind of what I figured was happening. Basically, my master struct is made up of a number of smaller structs. The using macros is an interesting idea; I'll give it some more thought.
–
Jim FellOct 19 '09 at 20:38

I end up relying on a library I wrote a long time ago to handle bit fields; specifically, variable-length ones. Very handy for variable-length compression. Most people I know of who need any serious bit manipulation end up rolling their own because nobody seems to have bothered to make a decent library.
–
std''OrgnlDaveApr 25 '12 at 17:43

In standard C language bit-fields can only be defined with a restricted set of types. In C89/90 these types are limited to int, signed int and unsigned int (a little-known detail is that in this context int is not guaranteed to be equivalent to signed int). In C99 type _Bool was added to the supported set. Any other types cannot be used in bit-field declaration.

In practice, as a popular extension, compilers normally allow any integral type (or also enum type) in bit-field declaration. But a struct type... No, I'm not aware of any compiler that would allow that (let alone that it doesn't seem to make much sense).