list_entry的用法
struct number
{
int num;
struct list_head list;
};
int main(void) {
struct number *tmp;
struct list_head *pos,*n;
int num_i;
//定义并初始化一个链表头
struct number number_head;
INIT_LIST_HEAD(&number_head.list);
for(num_i=0;num_i<10;num_i++)
{
tmp=(struct number*)malloc(sizeof(struct number));
tmp->num=num_i;
//吧这个节点链接到链表后面
//这里因为每次的节点都是加在number_head的后面,所以先进来的节点就在链表的最后面
list_add(&(tmp->list),&(number_head.list));
}
//下面把这个链接表中各个节点的值打印出来
printf("\n");
printf("-----------the list------------");
list_for_each(pos,&number_head.list)
{
tmp = list_entry(pos, struct number, list);
printf("number: %d \n",tmp->num);
}
return 0;
}
其中list_entry的定义如下
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
头文件就是加了个linux下的list.h
相关推荐
Linux内核list&hlist;解读
目录 1. 前言 2 2. 通用宏 2 2.1. typeof 2 2.1.1. 定义 3 2.1.2. 用途 3 2.1.3. 示例 3 2.2. offset_of 3 2.2.1. 定义 3 2.2.2. 作用 3 2.2.3. 原理 3 2.2.4. 示例 3 2.3. container_of 4 2.3.1. 定义 4 2.3.2. 作用 4 2.3.3. 示例 4 2.4. prefetch 4 2.4.1. 定义 4 2.4.2. 作用 4 3. list 5 3.1. list结构 5 3.1.1. 定义 5 3.1.2. 作用 5 3.1.3. 解读 5 3.1.4. 示例 5 3.2. 遍历方向 6 3.3. list_entry 6 3.3.1. 定义 6 3.3.2. 作用 7 3.4. list_for_each 7 3.4.1. 定义 7 3.4.2. 作用 7 3.4.3. 示例 7 3.5. __list_for_each 8 3.5.1. 定义 8 3.5.2. 作用 8 3.6. list_for_each_prev 8 3.6.1. 定义 8 3.6.2. 作用 8 3.7. list_for_each_safe 8 3.7.1. 定义 8 3.7.2. 作用 9 3.7.3. 区别 9 3.7.4. 示例 9 3.8. list_for_each_entry 9 3.8.1. 定义 9 3.8.2. 作用 10 3.8.3. 区别 10 3.8.4. 完整示例 10 3.9. list_for_each_entry_safe 12 3.9.1. 定义 12 3.9.2. 作用 13 3.10. list_for_each_entry_reverse 13 3.10.1. 定义 13 3.10.2. 作用 13 3.11. list_for_each_entry_continue 13 3.11.1. 定义 13 3.11.2. 作用 13 3.11.3. 区别 13 3.12. list_for_each_safe_rcu 14 4. hlist(hash list) 14 4.1. hlist(hash list)结构 14 4.1.1. 简述 14 4.1.2. 定义 14
内核数据结构之双向循环链表
从初学者的角度深入的分析了内核数据结构的双向循环链表 list_head结构如何构建链表 分析了list_entry LIST_HEAD list_for_each 并最后给出了一个例子以便学习
jstl之map,list访问遍历以及el表达式map取值的实现
下面小编就为大家带来一篇jstl之map,list访问遍历以及el表达式map取值的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Linux如何使用libudev获取USB设备VID及PID
在本文将使用libudev库来访问hidraw的设备。通过libudev库,我们可以查询设备的厂家ID(Vendor ID, VID),产品ID(Product ID, PID),序列号和设备字符串等而不需要打开设备。进一步,libudev可以告诉我们在/dev目录下设备节点的具体位置路径,为应用程序提供一种具有足够鲁棒性而又和系统厂家独立的访问设备的方式。使用libudev库,需要包含libudev.h头文件,并且在编译时加上-ludev告诉编译器去链接udev库。 将列出当前连接在系统中的所有hidraw设备,并且输出它们的设备节点路径、生产商、序列号等信息。 为了获取这些信息,需要创建
flask_mini_project
与flask / MongoDB mini项目一起工作的工作区。


