|
书上都是说struct中的成员都是按顺序存储的,总的大小就是各个成员大小之和。
以前从来都没怀疑过这个问题。
可是我刚刚测试了一下:
- #include <stdio.h>
- struct str
- {
- int b;
- char c[10];
- };
- struct str2{int a; int b; int c[10];};
- int
- main()
- {
- struct str aa;
- printf("size of struct a : %d = %d + %d\n", sizeof(struct str), sizeof(aa.b), sizeof(aa.c));
- printf("size of struct2 : %d\n", sizeof(struct str2));
- return 0;
- }
复制代码
输出结果是:
size of struct a : 16 = 4 + 10
size of struct2 : 48
这是咋回事? |
|