list_entry的用法

LLL_mj LLL_mj 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我编了一个小程序 在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取值的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

weixin_38684892

Linux如何使用libudev获取USB设备VID及PID

在本文将使用libudev库来访问hidraw的设备。通过libudev库,我们可以查询设备的厂家ID(Vendor ID, VID),产品ID(Product ID, PID),序列号和设备字符串等而不需要打开设备。进一步,libudev可以告诉我们在/dev目录下设备节点的具体位置路径,为应用程序提供一种具有足够鲁棒性而又和系统厂家独立的访问设备的方式。使用libudev库,需要包含libudev.h头文件,并且在编译时加上-ludev告诉编译器去链接udev库。 将列出当前连接在系统中的所有hidraw设备,并且输出它们的设备节点路径、生产商、序列号等信息。 为了获取这些信息,需要创建

weixin_38677190

flask_mini_project

与flask / MongoDB mini项目一起工作的工作区。

李韩资
评论 2
收藏 已收藏
分享
返回顶部
LLL_mj
Linux/Unix社区 18,828 成员 11,488 内容