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

#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);		//0
	int b = find(test, b);		//4
	int c = find(test, c);		//8
	int d = find(test, d);		//12

	cout << a << endl << b << endl << c << endl << d << endl;

	cout << sizeof(test) << endl;	//16

	system("pause");
	return 0;
}


posted on 2012-10-22 14:26  linzuxin  阅读(210)  评论(0编辑  收藏  举报

导航