NewbieWantsToLearn
NewbieWantsToLearn

Reputation: 3

singly linked list on C - warning C4047

I tried to create a list containing 20 tables. Each table should have: - a number in order from 1 to 20; - number of seats for each table (for table 1-10 - 2 seats; table 11-15 - 4 seats; table 16-20 - 6 seats); - and it should indicate weather it is occupied or not.

I need this list in my program, containing this info, so that I can later look for tables with n amount of seats, changing the status of the table from free to occupied, etc.

I think something is wrong in my code. I am getting the following warning:

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

My guess is I have done a newbie mistake and I have missunderstood the online toturials on lists in C. Any help would be greatly appreciated. What I have done so far is:

#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;

}

Upvotes: 0

Views: 64

Answers (1)

Marievi
Marievi

Reputation: 5009

You have the statement :

typedef struct tableList *table;

which means that table is a pointer to struct tableList, So pointer next is a pointer to table or a pointer to a pointer to struct tableList, or struct tableList **next, but then you assign to it values of struct table.

Change your code to :

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

Now, tableList is a pointer showing to a struct table, and therefore you can use it as a pointer to the first node of a list.

Read about your warning here.


In general, in order to create a single linked list, you should first define a node that looks like this :

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

and then if you want, define a pointer to your node :

typedef struct node *NodePtr;

Then you can dynamically allocate memory to create a node :

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

Now NodePtr points to a struct node.

Upvotes: 1

Related Questions