In the Below structure
struct SpecialFlags{
int id;
char flag1;
char flag2;
char flag3;
char flag4;
};
id flag1 flag2 flag3 flag4
--------------------------------------------------
| 99 | 1(49) | 2(50) | 3(51) | 4(52) |
---------------------------------------------------
0x100 0x104 0x105 0x106 0x107 0x108 <--lets say starting address is 0x100
When ClearFlags()
is called st
points to beginning of the structure. And when you do *(long*)&(st->flag1) = 0;
First address
of st->flags1(char type)
gets converted to long*
type that means it will fetch 4 bytes
at a time and finally *
means value in that 4 bytes.
From above figure
(long*)&(st->flag1)
=> from 0x104
address till next 4 byte i.e till 0x108
*(long*)&(st->flag1)
=> what value is there in between 0x104
location to 0x108
location
*(long*)&(st->flag1) = 0
=> what value is there in between 0x104
location to 0x108
location, will be overwritten with 0(zero)
After this statement your structure looks like
id flag1 flag2 flag3 flag4
--------------------------------------------------
| 99 | 0(48) | 0(48) | 0(48) | 0(48) |
---------------------------------------------------
0x100 0x104 0x105 0x106 0x107 0x108
Now when you prints flags.flag1
, flags.flag2
.. it prints 0
.