简体   繁体   中英

Dereferencing pointer to incomplete type queue in C

I want to make a queue using nodes, since it will be empty upon creation i want head and tail to point to NULL. But when i try to do it i get the error in the title. Any ideas?

 typedef struct node node_t;
 typedef struct queue queue_t;


 struct node{
        void *info;
        struct node *next;
 };

 struct queue{
        node_t *head;
        node_t *tail;
 };

queue_t* new_queue(void){
        queue_t* q = malloc(sizeof(queue_t));
        if (q == NULL)
                return NULL;
        q->tail = NULL;
        q->head = NULL;
        return q;
}

Thanks!

EDIT: this is the error i get when i try to compile this:

(gcc queue.c -Wall -pedantic -std=c99 -g -oq)

unt.c: In function 'new_queue':

unt.c:30:31: error: invalid application of 'sizeof' to incomplete type 'queue_t'

queue_t* q = malloc(sizeof(queue_t));

                           ^

unt.c:33:6: error: dereferencing pointer to incomplete type

q->tail = NULL; ^

unt.c:34:6: error: dereferencing pointer to incomplete type

q->head = NULL; ^

unt.c:35:2: error: expected declaration or statement at end of input

return q; ^

For me it compiles just fine. try to move the typedefs below the struct declaration

 struct node{
    void *info;
    struct node *next;
  };

 struct queue{
    node_t *head;
    node_t *tail;
 };

 typedef struct node node_t;
 typedef struct queue queue_t;

other than that it seems ok

EDIT: also the stdlib and stdio includes should be cheked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM