满意答案
k1ojstfs9y
推荐于 2016.02.20
采纳率:41% 等级:12
已帮助:13454人
都是一些小技巧
将0转化成结构体起始地址再取成员地址:&((类型 *)0->成员)
别忘了了取址符号,否则内存读写错误。
#include
#define MY_OFFSET(m,y) (&((m*)0)->y)
typedef struct _my_struct
{
int a;
int b;
int c;
}mystruct;
int main()
{
printf("a's offset of mystruct is %d\n",&((mystruct*)0)->a);
printf("b's offset of mystruct is %d\n",&((mystruct*)0)->b);
printf("b's offset of mystruct is %d\n",&((mystruct*)0)->c);
printf("a's offset of mystruct is %d\n",MY_OFFSET(mystruct,a));
printf("b's offset of mystruct is %d\n",MY_OFFSET(mystruct,b));
printf("b's offset of mystruct is %d\n",MY_OFFSET(mystruct,a));
return 0;
}
00分享举报