Don'ts for bit-fields

suggest change
  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 bit-field's type must be wide enough to contain 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 create a typedef for a structure containing bit-fields.

Whether unsigned char is allowed as a bit-field type is implementation-defined. A width larger than that type, such as : 20 on an 8-bit unsigned char, is invalid.

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 */
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents