/*
* offsetof是一个宏定义,可用于求结构体变量中某个成员(member)在整个结构体(类型为type)中的偏移量
*
*/
#include<iostream>
using namespace std;
typedef struct node_s node_t;
struct node_s
{
int a;
int b;
double c;
node_t *d;
};
#define myoffsetof(type, member) (size_t)&(((type *)0)->member)
int main(void)
{
cout<<myoffsetof(node_t, a)<<endl;
cout<<myoffsetof(node_t, b)<<endl;;
cout<<myoffsetof(node_t, c)<<endl;;
cout<<myoffsetof(node_t, d)<<endl;;
//printf("%s\n%s\n%d\n",__func__,__FILE__,__LINE__);
return 1;
}
/*
*输出如下:
*0
*4
*8
*16
*/
offsetof实现方式
最新推荐文章于 2025-05-29 22:40:00 发布