Don'ts for bit-fields
suggest change- Arrays of bit-fields, pointers to bit-fields, and functions returning bit-fields are not allowed.
- The address operator (
&) cannot be applied to bit-field members. - The bit-field's type must be wide enough to contain the field.
- The
sizeofoperator cannot be applied to a bit-field. - There is no way to create a
typedeffor a bit-field in isolation, though you can create atypedeffor 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 */
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents