offset求结构体成员的偏移量
【代码】
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/*
version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/25 */ #include "stdafx.h" #include <iostream> using namespace std; #define OFFSET(s,m) ((int)(&(((s*)0)->m))) struct MyStruct { int a; int b; int c; }; void test_offset() { cout << OFFSET(MyStruct, a) << endl; // 0 cout << OFFSET(MyStruct, b) << endl; // 4 cout << OFFSET(MyStruct, c) << endl; // 8 } int main() { test_offset(); return 0; } |