关于用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;
}
想问下前辈几个问题
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;
}
相关文章推荐
- 关于结构体中如何计算的问题
- 由结构体成员地址计算结构体地址——节点地址的函数list_entry()原理详解
- 由结构体成员地址计算结构体地址——list_entry()原理详解
- 关于结构体struct的sizeof计算问题
- 关于内存问题 WCHAR:计算长度,与安全连接
- 关于base_addr和frame_addr的地址的计算
- 关于网站地址解析到65.49.2.178的问题和解决方法
- Java 关于使用Arrays.asList()数组转集合的问题
- keil中关于使用_at_绝对地址定位问题
- 关于在list中按中文排序问题
- C++,关于指针形参”值传递VS地址传递“的问题
- 关于String地址引用的问题
- 关于android2.2地图定位的问题,程序员取得了设备所在经纬度,得不到具体的地址。这里做一下总结。
- 关于结构体的内存分配问题--sizeof(/*struct*/A)
- 关于freeMarker得到list长度的问题
- list转json的时候,关于date转换的问题
- 关于List,ArrayList中remove(object obj)方法不能删除对象问题
- list_head双向链表的删除问题
- 关于mybatis读取数据库字段text类型时,读出数据为地址,并不是字符串的问题
- javascript中值传递,地址传递,引用传递的问题(使用js创建list对象时会用到)