在C宏中,&符号的应用是什么?

Mil*_*avi 5 c macros linked-list linux-kernel

我正在阅读linux/list.h标题,它有这个宏:

#define LIST_HEAD_INIT(name) { &(name), &(name) }
Run Code Online (Sandbox Code Playgroud)

我想知道什么时候我写LIST_HEAD_INIT(birthday_list)宏如何扩展?

Vik*_*ngh 8

LIST_HEAD_INIT用于初始化列表头结构实例.

#define LIST_HEAD_INIT(name) { &(name), &(name) } 
#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)
Run Code Online (Sandbox Code Playgroud)

来自linux/types.h:

struct list_head {
    struct list_head *next, *prev;
};
Run Code Online (Sandbox Code Playgroud)

这扩展到了

struct list_head name = { &(name), &(name) }
Run Code Online (Sandbox Code Playgroud)

如您所见,它被扩展,现在结构实例"name"的"prev"和"next"指针字段指向自身.这是列表头的初始化方式.

初始化后LIST_HEAD(birthday_list)是birthday_list.prev = birthday_list.next =&birthday_list"birthday_list"是双链接列表的头节点,它是空的,而不是将prev和next指针留给NULL,它们被设置为指向返回头节点.

struct list_head birthday_list = {
    .next = &birthday_list,
    .prev = &birthday_list
}
Run Code Online (Sandbox Code Playgroud)