结构体的偏移量。在线等。
#include<stdio.h>
#include<stdlib.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
struct test_struct{
char ch;
int it;
int ul;
};
int main()
{
size_t off=-1; //unsignde int -1=>10...01
struct test_struct *st,*rt;
st = (struct test_struct *) malloc (sizeof(struct test_struct));
st->ch='a';
st->it=1;
st->ul=1ul;
// off =(size_t)&(((struct test_struct *)0)->it);
off=offsetof(struct test_struct,it);
// rt=(struct test_struct *)((char *)(&st->it)-off);
rt=(struct test_struct *)(((char *)&(st->it)-(char *)off));
printf("%d %d %d",rt->it,off,sizeof(struct test_struct));
return 0;
}
1、求结构体的偏移量是什么意思?没看懂。运用在哪些地方?
2、这个宏没看明白。能详细解释下嘛?size_t 取值符号?