C 上的单链表 - 警告 C4047

singly linked list on C - warning C4047

我试图创建一个包含 20 table 的列表。每个 table 应该有: - 一个从 1 到 20 的数字; - 每个 table 的座位数(table 1-10 - 2 个座位;table 11-15 - 4 个座位;table 16-20 - 6 个座位); - 它应该表明它是否被占用的天气。

我的程序中需要这个列表,其中包含此信息,这样我以后就可以查找 table 个有 n 个席位的人,将 table 的状态从空闲更改为已占用等

我认为我的代码有问题。 我收到以下警告:

warning C4047: '=': 'table *' differs in levels of indirection from 'table *'

我的猜测是我犯了一个新手错误,我误解了 C 列表中的在线教程。任何帮助将不胜感激。 到目前为止我所做的是:

#include<stdio.h>


typedef struct tableList *table;
struct table {
    int numTable; // number of table
    int numPeople;
    int free; //0 - free; 1 - occupied
    table *next;
};



int main(void) {
    struct table a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ;
    a.numTable = 1; a.numPeople = 2; a.free = 0;
    b.numTable = 2; b.numPeople = 2; b.free = 0;
    c.numTable = 3; c.numPeople = 2; c.free = 0;
    d.numTable = 4; d.numPeople = 2; d.free = 0;
    e.numTable = 5; e.numPeople = 2; e.free = 0;
    f.numTable = 6; f.numPeople = 2; f.free = 0;
    g.numTable = 7; g.numPeople = 2; g.free = 0;
    h.numTable = 8; h.numPeople = 2; h.free = 0;
    i.numTable = 9; i.numPeople = 2; i.free = 0;
    j.numTable = 10; j.numPeople = 2; j.free = 0;
    k.numTable = 11; k.numPeople = 4; k.free = 0;
    l.numTable = 12; l.numPeople = 4; l.free = 0;
    m.numTable = 13; m.numPeople = 4; m.free = 0;
    n.numTable = 14; n.numPeople = 4; n.free = 0;
    o.numTable = 15; o.numPeople = 4; o.free = 0;
    p.numTable = 16; p.numPeople = 6; p.free = 0;
    q.numTable = 17; q.numPeople = 6; q.free = 0;
    r.numTable = 18; r.numPeople = 6; r.free = 0;
    s.numTable = 19; s.numPeople = 6; s.free = 0;
    t.numTable = 20; t.numPeople = 6; t.free = 0;

    a.next = &b;
    b.next = &c;
    c.next = &d;
    d.next = &e;
    e.next = &f;
    g.next = &g;
    h.next = &i;
    i.next = &j;
    j.next = &k;
    k.next = &l;
    l.next = &m;
    m.next = &n;
    n.next = &o;
    o.next = &p;
    p.next = &q;
    q.next = &r;
    r.next = &s;
    s.next = &t;
    t.next = NULL;

}

你有声明:

typedef struct tableList *table;

表示table是指向struct tableList的指针,所以指针next是指向table的指针或者指向[=16=的指针] 或 struct tableList **next,但随后您为其分配值 struct table

将您的代码更改为:

struct table {
    int numTable; // number of table
    int numPeople;
    int free; //0 - free; 1 - occupied
    struct table *next;
};
typedef struct table *tableList;

现在,tableList 是一个指向 struct table 的指针,因此您可以将它用作指向列表第一个节点的指针。

了解您的警告 here


一般来说,为了创建一个单链表,你应该首先定义一个如下所示的节点:

struct node {
    ...                      //whatever you want your node to contain
    struct node *next;
}

然后,如果需要,定义指向节点的指针:

typedef struct node *NodePtr;

然后就可以动态分配内存创建节点了:

NodePtr = malloc(sizeof(struct node));
if (NodePtr == NULL)
    ...

现在NodePtr指向一个struct node.