Why this union's size is 2 with bitfields?

前端 未结 6 1243
独厮守ぢ
独厮守ぢ 2021-01-06 15:14

I am working on turbo C on windows where char takes one byte.Now my problem is with the below union.

union a
{
 unsigned char c:2;
}b;
void main()
{
printf(\         


        
6条回答
  •  花落未央
    2021-01-06 16:05

    I'm not sure where you find the requirement that the union must be precisely the minimum size. An object must be at least as big as its members, but that is a lower bound only.

    You can't take the address of a bitfield; what would be its type? It can't be int*. scanf(%d) will write sizeof(int) * CHAR_BIT bits to the int* you pass in. That's writing more than 2 bits, yet you don't have that space.

提交回复
热议问题