您的位置:首页 > 其它

关于用list_head 来计算结构体地址的问题

2013-03-25 22:18 246 查看
小弟根据网上的资料,写了这个代码来计算结构体world的地址。

想问下前辈几个问题

1)&((struct hello *)ptr)->list   ,这个表达式网上说法看不怎么懂,帮忙解释一下吧
2)为何地址前要加(char *)和unsigned long     ,我试了下不加的,结果最后一个出问题了,什么原因呢?



#include<stdio.h>

struct list_head
{
struct list_head *prev;
struct list_head *next;
};

struct hello
{
int a;
int b;
struct list_head list;
}world;

int main()
{
int ptr = 0;

struct list_head *p = &world.list;
printf("%p\n",&((struct hello *)ptr)->list);
printf("%p\n",(unsigned long)&((struct hello *)ptr)->list);
printf("%p\n",p);
printf("%p\n",(char *)p);
printf("%p\n",p - &((struct hello *)ptr)->list);
printf("%p\n",(char *)p - (unsigned long)&((struct hello *)ptr)->list);

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