您的位置:首页 > 大数据 > 人工智能

offsetof、container_of 实现

2015-09-08 16:20 387 查看
#include<stdio.h>
#include<string.h>
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({                      \
const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
(type *)( (char *)__mptr - offsetof(type,member) );})

#define  pppp ({ pp = 5; pp -3 ;})

struct data{
int a;
char b;
char c[10];

};

#define test struct data
int main()
{
struct data loc;
int pp = 4;
loc.a = 3;
char dd[10] = "baibin";
strcpy(loc.c,dd);
struct data *pdata ;
pdata = container_of(&(loc.b),struct data,b);
printf("a=%d, c=%s",pdata->a,pdata->c);
pp = ( {pp= 5; pp-2;});
printf("\n%d\n",pp);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: