您的位置:首页 > 其它

offset求结构体成员的偏移量

2014-09-25 11:25 183 查看
【代码】

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;

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