在C中取消引用指向不完整类型队列的指针

时间:2014-09-15 14:42:04

标签: c pointers null queue void

我想使用节点创建一个队列,因为它在创建时将为空,我希望head和tail指向NULL。但是当我尝试这样做时,我会在标题中得到错误。有什么想法吗?

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

谢谢!

编辑:这是我尝试编译时遇到的错误:

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

unt.c:在函数'new_queue'中:

unt.c:30:31:错误:'sizeof'无效应用于不完整类型'queue_t'

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

                           ^

unt.c:33:6:错误:取消引用指向不完整类型的指针

q-> tail = NULL;       ^

unt.c:34:6:错误:取消引用指向不完整类型的指针

q-> head = NULL;       ^

unt.c:35:2:错误:输入结束时的预期声明或声明

返回q;   ^

1 个答案:

答案 0 :(得分:1)

对我来说,它编译得很好。尝试将typedef移动到struct声明

下面
 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;

除此之外似乎没问题

编辑: stdlib和stdio包括应该被解雇。