#define offsetof(s,m) (unsigned long)&(((s *)0)->m)
//->的優(yōu)先級比&要高
struct list{
char a;
int b;
short c;
};
typedef struct list List;
int main()
{
unsigned char *child = NULL;
unsigned char *owner = NULL;
unsigned char *final = NULL;
List test;
child = (unsigned char *)&(test.b);//將地址轉(zhuǎn)換為unsigned char *型的指針,賦給child
owner = (unsigned char *)&test;
printf("the address of struct is %p\n",owner);
printf("the address of b is %p\n",child);
unsigned long offset = offsetof(List,b);
printf("offset is %p\n",offset);
final = (unsigned char *)child - offset;//結(jié)構(gòu)成員的地址減去偏移值,得到結(jié)構(gòu)體實例的起始地址。
printf("after sub offset,the struct address is %p\n",final);
return 0;
}