简体   繁体   English

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

[英]What is the application of ampersand within C macros?

I'm reading linux/list.h header, it have this macro : 我正在读linux/list.h头文件,它有这个宏

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

I want to know when I write LIST_HEAD_INIT(birthday_list) how the macro expanded? 我想知道什么时候写LIST_HEAD_INIT(birthday_list)宏如何扩展?

LIST_HEAD_INIT is used to initialize the list head structure instance. LIST_HEAD_INIT用于初始化列表头结构实例。

#define LIST_HEAD_INIT(name) { &(name), &(name) } 
#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)

from linux/types.h: 来自linux / types.h:

struct list_head {
    struct list_head *next, *prev;
};

This is expanded to 这扩展到了

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

As you can see, it is expanded and now the "prev" and "next" pointers fields of structure instance "name" points back to itself. 如您所见,它被扩展,现在结构实例“name”的“prev”和“next”指针字段指向自身。 This is how the list head is initialized. 这是列表头的初始化方式。

After intialization LIST_HEAD(birthday_list) is birthday_list.prev = birthday_list.next = &birthday_list "birthday_list" is the head node of the double linklist which is empty and instead of leaving the prev and next pointer to NULL, they have been set to point back to the head node. 初始化后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
}

There's nothing special about ampersands, they're just another token. &符号并没有什么特别之处,它们只是另一种象征。 LIST_HEAD_INIT(birthday_list) gets expanded as { &(birthday_list), &(birthday_list) } LIST_HEAD_INIT(birthday_list)扩展为{ &(birthday_list), &(birthday_list) }

You can just look at the output of the preprocessor directly if you want to check this or other macro expansions yourself. 如果要自己检查此扩展或其他宏扩展,可以直接查看预处理器的输出。 GCC has the -E argument to do this. GCC有-E参数来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM