打印结构体偏移

转载自:http://blog.csdn.net/kelsel/article/details/53506283

#define OFFSET(x, y) &((x *)0)->y

struct TestStruct { int mVar1; int mVar2; int mVar3;};

void OffsetTestStruct(){  

fprintf(stderr, "sizeof(struct TestStruct) %d\n", sizeof(struct TestStruct));  

fprintf(stderr, "-- mVar1 offset : %d\n", OFFSET(TestStruct, mVar1));  

fprintf(stderr, "-- mVar2 offset : %d\n", OFFSET(TestStruct, mVar2));  

fprintf(stderr, "-- mVar3 offset : %d\n", OFFSET(TestStruct, mVar3));

}

你可能感兴趣的:(c++)