Explanation of the output of this program (bit-bit in the structure)

#include <stdio.h> int main() { struct bitfield { unsigned a:5; unsigned c:5; unsigned b:6; } bit; char *ptr; struct bitfield bit1={1,3,3}; ptr=&bit1; ptr++; printf("%d",*ptr); return 0; } 

The answer to this question is 12. How does this happen? Can anyone explain this? I tried my best to explain it.

+4
source share
4 answers

The explanation for this question is quite SIMPLE:

 Binary value of 1 is 00001 (as "a" have 5 bitfield) Binary value of 3 is 00011 (as "c" have 5 bitfield) Binary value of 3 is 000011 (as "b" have 6 bitfield) 

The memory layout can be visualized as follows: enter image description here

The first 5 bits are occupied by a and have the value 00001. Then, 5 bits are occupied by b , having the value 00011 and the last 6 bits, by c with the value 000011.

So, at startup, the ptr is in memory location 1000, and now that you have done ptr++ . Since sizeof(char) is 1, ptr will move to 1 memory location. Therefore, ptr moves to memory location 1001.

Therefore, *ptr will give you the value stored in memory cell 1001, and therefore the answer will be 12

+14
source

How will 1 be displayed using 5 bits? This will be 00001 , and 3 will be 00011 (Note that b has 6 bit fields, so it will have an extra zero: 000011 ).

Now suppose the address of bit1 is 1000. What will ptr++ ? This will be 1001 (since sizeof ptr is 1).

What does *ptr mean? This means the contents of location 1001, which will be 00001100.

binary 00001100 is decimal 12 .

+2
source

After initialization, the bit field has the following values:

 000011 00011 00001 ^^^^^^ ^^^^^ ^^^^^ b = 3 c = 3 a = 1 

Assuming char is 8 bits wide, you can split 16 bits into two 8-bit parts:

 00001100 01100011 ^^^^^^^^ ^^^^^^^^ ptr + 1 ptr 

So you will print an octet on ptr + 1, which is 12.

However, I am sure that this causes undefined behavior, since the address of the bit fields should not be used (not to mention the alias via a pointer, which is even of an incompatible type ...)

+2
source

A program to demonstrate where a, b, and c files are stored. Please note that he is a little confused due to endianness.

 #include<stdio.h> #include<string.h> struct bitfield { unsigned a:5; unsigned c:5; unsigned b:6; }; void print_bitfield(unsigned a, unsigned c, unsigned b) { struct bitfield bf; memset(&bf, 0, sizeof(bf)); bf.a = a; bf.b = b; bf.c = c; unsigned char* ptr = (unsigned char*)&bf; unsigned i; printf("%2x %2x %2x: ", a, c, b); for (i = 0; i < sizeof(bf); i++) { printf("%02x ", ptr[i]); } printf("\n"); } int main() { printf("sizeof bitfield: %u\n",sizeof(struct bitfield)); printf(" acb: 0 1 2 3\n"); print_bitfield(0, 0, 0); print_bitfield(1, 0, 0); print_bitfield(31, 0, 0); print_bitfield(0, 1, 0); print_bitfield(0, 31, 0); print_bitfield(0, 0, 1); print_bitfield(0, 0, 63); print_bitfield(1, 3, 3); return 0; } 

Output:

 sizeof bitfield: 4 acb: 0 1 2 3 0 0 0: 00 00 00 00 1 0 0: 01 00 00 00 1f 0 0: 1f 00 00 00 0 1 0: 20 00 00 00 0 1f 0: e0 03 00 00 0 0 1: 00 04 00 00 0 0 3f: 00 fc 00 00 1 3 3: 61 0c 00 00 
+2
source

Source: https://habr.com/ru/post/1486456/


All Articles