首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

定义一个宏获取构造体中变量相对结构体开始的偏移量

2012-11-09 
定义一个宏获取结构体中变量相对结构体开始的偏移量#include iostreamusing namespace stdstruct test{

定义一个宏获取结构体中变量相对结构体开始的偏移量

#include <iostream>using namespace std;struct test{int a;char b;int c;char d;};#define find(type, var) (unsigned int)(&(((type *)(0))->var))int main(){int a = find(test, a);//0int b = find(test, b);//4int c = find(test, c);//8int d = find(test, d);//12cout << a << endl << b << endl << c << endl << d << endl;cout << sizeof(test) << endl;//16system("pause");return 0;}


 

热点排行