这个静态结构的目的是什么?

What's the purpose of this static struct?

我看到了下面的代码,我对它的用途有点困惑。

struct bob{
    int myNum;
    struct bob * next;
};

static struct bob_stuff{
    int theNum;
    struct bob *lists;
} bob;

我知道第二个结构是静态的并且被初始化为 bob 结构,但你为什么要这样做?但我不太确定为什么你有 2 个结构。

这看起来像 "module"(或 "class",如果你愿意)维护一堆单链整数列表的状态。

当然命名很糟糕,应该是例如

struct list_node {
  int myNum;
  struct list_node *next;
};

static struct {
  int theNum;
  struct list_node *lists;
} listState;

请注意,名称 ("struct tag") bob_stuff 毫无意义且令人困惑,应该删除。如果你想要一个 static,因此是 struct 类型的局部变量,那么标签可能无论如何都没有用。

我认为是结构的名称让您感到困惑。

是单链表的定义。

第一个结构

struct bob{
    int myNum;
    struct bob * next;
};

定义列表的一个节点。这样会更清楚我会用不同的名字重写它

struct Node{
    int value;
    struct Node * next;
};

第二个结构简单地定义了列表的头部和列表中的节点数(我认为)

static struct bob_stuff{
    int theNum;
    struct bob *lists;
} bob;

所以可以改写成

static struct SingleLinkedList{
    int nodes_count;
    struct Node *head;
} list;

这个抽象列表用作一些 Bob 内容的容器。:)