c - C 上的单链表 - 警告 C4047

标签 c list initialization warnings singly-linked-list

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

我的程序中需要包含此信息的列表,以便我稍后可以查找有 n 个座位的 table ,将 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的指针或指向struct tableListstruct 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是一个指向结构表的指针,因此您可以将它用作指向列表的第一个节点的指针。

了解您的警告 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指向一个结构节点

关于c - C 上的单链表 - 警告 C4047,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020578/

相关文章:

c - 动态指针值分配

python - 用另一个列表的一些值 append 一个列表

python - 如何返回True和False?

c# - 使用 EqualityComparer 初始化静态字典

arrays - 警告 : initialization from incompatible pointer type - how to initialize array of struct containing function pointers

c++ - 为什么从字符串常量到 'char*' 的转换在 C 中有效但在 C++ 中无效

c - 为什么 scanf() 无法过滤双引号,尽管将格式设置为 [A-Za-z]

c - scanf 之后 fgets 不起作用

python - 从字符串列表中提取 8 位数字

C++ 检查未初始化的迭代器