可能重复:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?为什么这两个结构的大小不同?
#include<stdio.h>
struct csie {
char c;
short s;
int i;
double e;
};
struct ceis {
char c;
double e;
int i;
short s;
};
int main(void) {
printf("csie = %d\n", sizeof(struct csie));
printf("ceis = %d\n", sizeof(struct ceis));
return 0;
}
输出是:
CSIE = 16
CEIS = 24
正确的原因,错误的计算。 – 2011-02-25 05:36:29
您对csie的回答很有道理。你对ceis的回答不像总共20字节,他的输出是24? – Pete 2011-02-25 05:37:05
是的,我意识到短之后是2个字节。我也忘记了双打可以是8字节对齐。 – GWW 2011-02-25 05:37:20