简体   繁体   中英

Unusual syntax for bitfield declaration

I came across a bitfield syntax which I have never seen before.

       struct msg_hdr0{
                        uint8_t a  : 1,
                        b         : 1,
                        e         : 4,
                        f         : 2;                                                                                                                                                                       
               };

     int main(void)
     {
     struct msg_hdr0 hdr;

     bzero((void *)&hdr, sizeof(hdr));
     hdr.b = 1;

     printf("hdr = 0x%X\n", *(uint32_t *)&hdr);

     return 0;
     }

This works fine on linux & gcc compiler. Can anyone suggest where can I find any documentation on this. Is this a GCC extension ?

The common bitfield syntax is:

    struct box_props
   {
 unsigned int opaque       : 1;
 unsigned int fill_color   : 3;
 unsigned int              : 4; // fill to 8 bits
 unsigned int show_border  : 1;
 unsigned int border_color : 3;
 unsigned int border_style : 2;
 unsigned int              : 2; // fill to 16 bits
};

In a function you can declare a list of variables in a single statement, or in multiple statements.

void myFunction(void)
{
    // Declare several variables (of teh same type) in a single statement
    int a, b, c;
    // Declare some more, each in their own statement
    int x;
    int y;
    int z;
}

Similarly, bit fields in structs.

struct myStruct
{
    // Declare several bitfields in a single statement
    int a : 1, b : 3, c : 4;
    // Declare some more, each in their own statement
    int x : 1;
    int y : 3;
    int z : 4;
}

Standard C99 section $6.7.2.1 says as,

struct-declarator-list:
     struct-declarator
     struct-declarator-list , struct-declarator

struct-declarator:
     declarator
     declarator_opt : constant-expression

You can declare struct bit field members using , as,

   struct msg_hdr0 {
     uint8_t a : 1, b : 1, e : 4, f : 2;
   };

For your question how bit fields are used the below code might help. Bit fields are standard in C and are used when the whole size of the int is not required to store the value and a specific number of bits are enough to hold the value by doing so they save memory as shown below. There is already a link in the another answer as to why not use bit fields but for understanding the purpose is this.

#include <stdio.h>
struct a
{
    unsigned int a;
    unsigned int b;
};
struct b
{
    unsigned int a : 2; /* unsigned int a:2, */
                        /*              b:3;  */
    unsigned int b : 3;
};
int main(void) {
    struct b p;
    p.a = 3; /* This member can hold just 2 bits */
    p.b = 7;/* This member can hold just 3 bits */
    printf("%d\n",sizeof(struct a)); /* Size of struct is 8 bytes */
    printf("%d\n",sizeof(struct b)); /* size of struct is 4 bytes using bit fields*/
    printf("%d\n",p.a);
    printf("%d\n",p.b);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM