This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




已关闭8年。



struct a
{
    char *c;
    char b;
};

sizeof(a)是多少?

最佳答案

#include <stdio.h>

typedef struct { char* c; char b; } a;

int main()
{
    printf("sizeof(a) == %d", sizeof(a));
}

在32位计算机上,我得到“sizeof(a)== 8”。结构的总大小取决于打包:在我的情况下,默认打包为4,因此'c'占用4个字节,'b'占用一个字节,剩下3个填充字节将其带入下一个4的倍数。 :8.如果要更改此打包,大多数编译器都有一种方法可以更改它,例如,在MSVC上:
#pragma pack(1)
typedef struct { char* c; char b; } a;

给出sizeof(a)==5。如果执行此操作,请在任何库头文件之前小心重置包装!

关于c - 我如何找到结构的大小? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/143025/

10-11 23:04
查看更多