Why does this code contains colon in struct?

前端 未结 2 1536
野性不改
野性不改 2021-01-28 00:52

Please explain how this code is executing.why we has used \":\" in structures.what is the use of colon in structures.what should be the output of sizeof operator.



        
2条回答
  •  隐瞒了意图╮
    2021-01-28 01:11

    It's a part of syntax of bit fields. Here it means that a occupies 3 bits, b 13 bits and c only 1 bit. Of course the structure will not occupy only 17 bits in memory, as it must be aligned to bytes as the smallest addressable memory unit, so sizeof(bit1) will be at least 3 bytes (probably it will be aligned to some value related to a machine word, e.g. 4 bytes). You can read more about the alignment here: Structure padding and packing. I assumed that 1 byte is 8 bits size but there are some old or exotic architectures where bytes have another size.

提交回复
热议问题