我无法理解这用于实现静态链表的三行代码。这实际上就是这个question的答案.
我再次在这里发布代码-(主要操作基本上是第二行)
struct node {int x; struct node *next;};
#define cons(x,next) (struct node[]){{x,next}}
struct node *head = cons(1, cons(2, cons(3, cons(4, NULL))));
我的问题是 - 该语句的功能是什么?
(struct node[]){{x,next}}
。这是一个初始化语句吗?它返回什么,可以将其分配给struct node*
?
最佳答案
(struct node[]){{x,next}}
是一个复合文字,它将初始化一个 struct *node
指针。
+------+------+ +------+------+ +------+------+ +------+------+
| | | | | | | | | | | |
| 1 | next +---->| 2 | next +---->| 3 | next +---->| 4 | NULL |
| | | | | | | | | | | |
+------+------+ +------+------+ +------+------+ +------+------+
^
|
head
关于c - 这段代码实际上是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29196158/