#include<stdio.h>
struct test {
char a;
char b;
int c;
int d;
};
void main() {
int a,b;
char c,d;
printf("Address of a & b = %u & %u respectively\n",&a,&b);
printf("Address of c & d = %u & %u respectively\n",&c,&d);
struct test t1;
printf("The size of structure:::%d\n",sizeof(t1));
}
输出是:
Address of a & b = 3216087804 & 3216087808 respectively
Address of c & d = 3216087802 & 3216087803 respectively
The size of structure:::12
当我以这种方式声明结构时:
struct test {
char a;
int b;
int c;
char d;
};
在这种情况下输出:
The size of structure:::16
为什么当我们尝试访问内存中奇数位置的 secord char 变量或地址为 4 的倍数时不存在的变量时不会发生对齐错误?