sizeof(b)
will give you the size in bytes of a variable of type struct tB
,which in this case will be 4 (Due to padding
it won't be 3 as it is expected to be)
sizeof(*p)
will again give you the size in bytes of a variable of type struct tB
.You should initialize p
with the address of a variable of struct tB
type.Eg:
struct tB *p=&b;
But you should know that in this case if you use sizeof(p)
then it would give the size of the pointer p
, not the variable pointed by p
. Try this variation of your program :
#include<stdio.h>
struct tB
{
unsigned b1:3;
signed b2:6;
unsigned b3:11;
signed b4:1;
unsigned b5:13;
} b;
int main(void)
{
struct tB *p;
printf("%d\n%d",sizeof(*p),sizeof(p));
}
Here is another variation that rounds the size of struct tB
to 24 bits(3 bytes) as you expect,by dealing with the padding using the #pragma pack()
directive,which is compiler dependent (I am using CodeBlocks on Windows).
#include<stdio.h>
#pragma pack(1)
struct tB
{
unsigned b1:3;
signed b2:6;
unsigned b3:11;
signed b4:1;
} b;
int main(void)
{
struct tB *p;
printf("%d\n%d",sizeof(*p),sizeof(p));
}