C Language Don'ts for bit-fields


Example

  1. Arrays of bit-fields, pointers to bit-fields and functions returning bit-fields are not allowed.
  2. The address operator (&) cannot be applied to bit-field members.
  3. The data type of a bit-field must be wide enough to contain the size of the field.
  4. The sizeof() operator cannot be applied to a bit-field.
  5. There is no way to create a typedef for a bit-field in isolation (though you can certainly create a typedef for a structure containing bit-fields).
typedef struct mybitfield
{
    unsigned char c1 : 20;   /* incorrect, see point 3 */
    unsigned char c2 : 4;    /* correct */
    unsigned char c3 : 1;
    unsigned int x[10]: 5;   /* incorrect, see point 1 */
} A;

int SomeFunction(void)
{
    // Somewhere in the code
    A a = { … };
    printf("Address of a.c2 is %p\n", &a.c2);      /* incorrect, see point 2 */
    printf("Size of a.c2 is %zu\n", sizeof(a.c2)); /* incorrect, see point 4 */
}