struct t{
char days[20];
int date;
char x;
struct t *next;
}*head
printf("%ld\n", sizeof(head));
其中sizeof(*void)=8
,sizeof(int)=4
,sizeof(char)=1
为什么sizeof()这个结构体8?
为什么它打印8?
struct t{
char days[20];
int date;
char x;
struct t *next;
}*head
printf("%ld\n", sizeof(head));
其中sizeof(*void)=8
,sizeof(int)=4
,sizeof(char)=1
为什么sizeof()这个结构体8?
为什么它打印8?
请注意,head
是指针而不是结构的实际实例。这意味着sizeof(head)
是指针的大小,在您的系统上恰好为8(注意sizeof(void*)
也是8)。
希望这会有所帮助!
所以它实际上并没有在结构中加入元素的大小。好的,谢谢 – user2821523
@ user2821523完全正确 - 只是询问指针占用了多少空间。 – templatetypedef
head
是一个指向struct t
的指针,它是8字节,因为我假设你正在运行一个x64程序。如果你想要的底层类型的大小,这样做:
sizeof(*head)
因为它的指针? – FDinoff
因为你忘记了星号。 'sizeof(* head)'会给你你正在寻找的东西。投票结束(错字)。 – dasblinkenlight