sizeof(b)
会给你在struct tB
类型的变量,在这种情况下将是4字节的大小(由于padding
它不会是3,因为它预期是)
sizeof(*p)
将再次给你struct tB
类型的变量的字节数。您应该struct tB
type.Eg的变量的地址初始化p
:
struct tB *p=&b;
但是你应该知道,在这种情况下,如果你使用sizeof(p)
吨如果它会给出指针p
的大小,而不是p
指向的变量。试试你的程序的这种变化:
#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));
}
这里是另一个变化是轮struct tB
24位(3个字节),你期望的那样,通过处理使用#pragma pack()
指令填充,这是依赖于编译器(大小我在Windows上使用CodeBlocks)。
#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));
}
谢谢!即时通讯不知道第二个声明是说什么? – RightLeftRight12 2013-04-30 06:03:05
@ user2127663让我编辑并清除。 – 2013-04-30 06:03:35
好的,如果我只是要求sizeof(p),它会给我4,因为它的指针是正确的? – RightLeftRight12 2013-04-30 06:05:20