通过结构体中成员的地址,找到结构体的地址

#include <stdio.h>
#include <string.h>
typedef struct _list_head{
struct list_head * next, * pre;
} list_head;

typedef struct _page{
int id;
list_head list1;
char name[64];
list_head list;
} page;

int main(){
page mypage;
strcpy(mypage.name,“hello world!”);

list_head * mylist = &mypage.list;//找到list的地址

page * page1 = (page *)((char *)(mylist)-(unsigned long)(&((page*)0)->list));//通过page结构体中list地址,找到page的地址
printf("%s\n",page1->name);
return 0;

}

猜你喜欢

转载自blog.csdn.net/katerdaisy/article/details/132578877