1.验证先定义的成员变量地址是低地址
2.验证__attribute__((packed))结构体的大小是紧凑模式没有填充,4+4+1+4=13个字节大小,32位GCC
#include "stdio.h"
typedef struct student{
int num;
int score;
char c;
struct student *next;
} __attribute__((packed)) st;
int main(void){
st a,b,*head,*p;
a.num=0x99101;
a.score=0x99;
b.num=0x99102;
b.score=0x80;
head=&a;
a.next=&b;
printf("%p,%p,%p,%p\n",&a.num,&a.score,&a.c,&a.next);
printf("%p,%p,%p,%p\n",&b.num,&b.score,&b.c,&b.next);
printf("%x,%x,%x,%x\n",sizeof(st),sizeof(a),sizeof(b),sizeof(*(a.next)));
return 0;
}
本次运行结果:
[root@andLinux win]# gcc sp.c -o sp &&./sp
0xbfbf965f,0xbfbf9663,0xbfbf9667,0xbfbf9668
0xbfbf9652,0xbfbf9656,0xbfbf965a,0xbfbf965b
d,d,d,d