Linux内核学习中---有关#define LIST_HEAD_INIT(name) { &(name), &(name) }的问题



最近在接触内核方面的东西,遇见如下一段代码:

<pre class="html" name="code">struct list_head {
        struct list_head *next, *prev;        //双向链表
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }  

#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)

#define INIT_LIST_HEAD(ptr) do { \
        (ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)


 

在网上看了很多前人工作,在这里自己做下总结。

 

来看数据结构体:

struct list_head {         struct list_head *next, *prev; }; 
//宏定义如下:
#define LIST_HEAD_INIT(name) { &(name), &(name) } 
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name) 


举例如下:

struct list_head foo = { &(foo) , &(foo)}

相当于:

struct list_head foo; foo.next = &foo; foo.prev = &foo;

另一个例子:

struct list_head test = LIST_HEAD (check); LIST_HEAD (check);

在C语言中我们使用的结构体对应实例:例如:

struct student{long int num;

char name[20];char sex;

char addr[20];

}a={10101,"Li yong tian",'M',“513477736”};

 

a的初始化是四项,与结构体的成员是一一对应的。而结构体中:

struct list_head foo = { &(foo) , &(foo)}

在本文中等价与

:struct list_head {        struct list_head *next, *prev; } foo = { &(foo) , &(foo)};

按照成员的对应赋值就是:

struct list_head foo; foo.next = &foo; foo.prev = &foo;

 

//如果我有一个定义了一个对象:
<span style="font-size: 16px;">struct list_head mylist;
 
//then

LIST_HEAD(mylist);
==
struct list_head mylist = { &(mylist), &(mylist) } ;

</span>

                
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值