开发者

how to get struct's start address from its member's address

In C language, how to get struct's st开发者_如何转开发art address from its member's address?

struct type1 {
    //...
    int member1;
    //...
};
struct type1 obj1;

And ptr1 is the address of member member1 in obj1, how to define

macro #define start_add(ptr1, type1, member1) to get obj1's

start address?


You can do this using offsetof:

#define start_add(ptr1, type1, member1) ((type1 *)((char *)(ptr1) - offsetof(type1, member1)))


this from windows wdk

/* 
 #define CONTAINING_RECORD(address, type, field) ((type *)( \
      (PCHAR)(address) - \
      (ULONG_PTR)(&((type *)0)->field)))
*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜