在 C 中静态初始化链表的最佳方法?

dpe*_*rcy 5 c c11

我有一个单链表类型,如下所示:

struct point { int x, y; };
struct point_list {
    struct point value;
    const struct point_list *next;
};
Run Code Online (Sandbox Code Playgroud)

我想静态初始化这些列表之一,可能有几十个条目。我想用一致的缩进每行写一个项目,以便于编辑列表。

到目前为止,我想出的最好的是:

const struct point_list *const my_list =
    &(const struct point_list) { .value = { 1, 2 }, .next =
    &(const struct point_list) { .value = { 3, 4 }, .next =
    &(const struct point_list) { .value = { 5, 6 }, .next =
    NULL
    }}};
Run Code Online (Sandbox Code Playgroud)

但缺点是:

  • 添加或删除项目时,我需要更新最后一行的右大括号的数量。
  • 可能很难说服代码格式化程序保持这种风格。

有没有更好的办法?

如果我们有递归宏,也许这样的事情可以工作:

const struct point_list *const my_list = POINT_LIST(
    ((struct point) { 1, 2 }),
    ((struct point) { 3, 4 }),
    ((struct point) { 5, 6 }),
);
Run Code Online (Sandbox Code Playgroud)

如果我们可以在编译时运行代码,也许这样的事情可以工作:

#define array_length(X) (sizeof(X) / sizeof(X[0]))

constexpr const struct point_list *array_to_list(size_t length, struct point *values) { ... }

const struct point my_array[] = {
    { 1, 2 },
    { 3, 4 },
    { 5, 6 },
};
const struct point_list *const my_list = array_to_list(array_length(my_array), my_array);
Run Code Online (Sandbox Code Playgroud)

120*_*arm 8

my_list您可以将其声明为数组,而不是声明为指针:

struct point_list const my_list[] = {
    { .value = { 1, 2 }, .next = &my_list[1] },
    { .value = { 3, 4 }, .next = &my_list[2] },
    { .value = { 5, 6 }, .next = NULL }
};
Run Code Online (Sandbox Code Playgroud)

如果你仍然想my_list成为一个指针,你可以做类似的事情:

static struct point_list const my_list_data[] = {
    // ...
};
const struct point_list *const my_list = my_list_data;
Run Code Online (Sandbox Code Playgroud)