C:sys / queue.h-如何将指向TAILQ_HEAD()的指针传递给函数,而不是使TAILQ_HEAD()成为全局函数?

时间:2018-07-11 16:14:00

标签: c queue

这里是C的新手。我正在使用sys/queue.h进行简单的排队。我已经在SO和Google上进行了大量搜索,但找不到针对该特定问题的解决方案。

这很好:

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h> 

TAILQ_HEAD(, q_item) head;

typedef struct q_item {
        int value;
        TAILQ_ENTRY(q_item) entries;
} q_item;

void enqueue(int n) {
    // enqueue the node with value n
    q_item *item;
    item = malloc(sizeof(q_item));
    item->value = n;
    printf("queued %d\n", item->value);
    TAILQ_INSERT_TAIL(&head, item, entries);
}

void dequeue() {
    q_item *returned_item;
    returned_item = TAILQ_FIRST(&head);
    printf("dequeued %d\n", returned_item->value);
    TAILQ_REMOVE(&head, returned_item, entries);
    free(returned_item);
}

int main() {

    TAILQ_INIT(&head);

    enqueue(1);
    enqueue(2);
    enqueue(3);

    dequeue();

    return 0;
}

我知道通常应该避免使用全局变量。尽管我也知道TAILQ_HEAD()是一个宏,但也许这改变了我对此的看法。无论如何,这不会编译:

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h> 

typedef struct q_item {
        int value;
        TAILQ_ENTRY(q_item) entries;
} q_item;

void enqueue(int n, TAILQ_HEAD(, q_item) * head) {
    // enqueue the node with value n
    q_item *item;
    item = malloc(sizeof(q_item));
    item->value = n;
    printf("queued %d\n", item->value);
    TAILQ_INSERT_TAIL(head, item, entries);
}

void dequeue(TAILQ_HEAD(, q_item) * head) {
    q_item *returned_item;
    returned_item = TAILQ_FIRST(head);
    printf("dequeued %d\n", returned_item->value);
    TAILQ_REMOVE(head, returned_item, entries);
    free(returned_item);
}

int main() {

    TAILQ_HEAD(, q_item) head; // <-- I've moved TAILQ_HEAD into main()
    TAILQ_INIT(&head);

    enqueue(1, &head);
    enqueue(2, &head);
    enqueue(3, &head);

    dequeue(&head);

    return 0;
}

当我尝试编译后者时,出现以下错误。他们无济于事,因为我无法分辨‘struct <anonymous> *’‘struct <anonymous> *’之间的区别。

test_tailq_noglobal.c:32:13: warning: passing argument 2 of ‘enqueue’ from incompatible pointer type [-Wincompatible-pointer-types]
      enqueue(1, &head);
                 ^
test_tailq_noglobal.c:10:6: note: expected ‘struct <anonymous> *’ but argument is of type ‘struct <anonymous> *’
     void enqueue(int n, TAILQ_HEAD(, q_item) * head) {
      ^~~~~~~

我知道TAILQ_HEAD的手册页显示您可以定义以下内容:

TAILQ_HEAD(tailhead, entry) head;
struct tailhead *headp;                 /* Tail queue head. */

但是我不确定该struct tailhead *headp怎么办。我尝试如下将其作为指针传递来代替&head,但这似乎也不起作用:

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h> 

typedef struct q_item {
        int value;
        TAILQ_ENTRY(q_item) entries;
} q_item;

void enqueue(int n, struct headname *headp) {
    // enqueue the node with value n
    q_item *item;
    item = malloc(sizeof(q_item));
    item->value = n;
    printf("queued %d\n", item->value);
    TAILQ_INSERT_TAIL(headp, item, entries);
}

void dequeue(struct headname *headp) {
    q_item *returned_item;
    returned_item = TAILQ_FIRST(headp);
    printf("dequeued %d\n", returned_item->value);
    TAILQ_REMOVE(headp, returned_item, entries);
    free(returned_item);
}

int main() {

    TAILQ_HEAD(headname, q_item) head;
    struct headname *headp;
    TAILQ_INIT(headp);

    enqueue(1, headp);
    enqueue(2, headp);
    enqueue(3, headp);

    dequeue(headp);

    return 0;
}

错误:

test_tailq_headp.c: In function ‘dequeue’:
test_tailq_headp.c:21:18: error: dereferencing pointer to incomplete type ‘struct headname’
  returned_item = TAILQ_FIRST(headp);
                  ^
test_tailq_headp.c: In function ‘main’:
test_tailq_headp.c:33:13: warning: passing argument 2 of ‘enqueue’ from incompatible pointer type [-Wincompatible-pointer-types]
  enqueue(1, headp);
             ^~~~~
test_tailq_headp.c:10:6: note: expected ‘struct headname *’ but argument is of type ‘struct headname *’
 void enqueue(int n, struct headname *headp) {
      ^~~~~~~

有人可以告诉我我在做错什么,无论是在我的代码中还是在我思考这个问题的方式上?谢谢。

1 个答案:

答案 0 :(得分:3)

我从不使用此队列,但是我发现的一种方法是在q_item自身内部添加TAILQ_HEAD()。它有助于避免全局使用head。

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>

typedef struct q_item {
    int value;
    TAILQ_ENTRY(q_item) entries;
    TAILQ_HEAD(, q_item) head;
} q_item;

void enqueue(int n, q_item *q) {
    // enqueue the node with value n
    q_item *item;
    item = malloc(sizeof(q_item));
    item->value = n;
    printf("queued %d\n", item->value);
    TAILQ_INSERT_TAIL(&q->head, item, entries);
}

void dequeue(q_item *q) {
    q_item *returned_item;
    returned_item = TAILQ_FIRST(&q->head);
    printf("dequeued %d\n", returned_item->value);
    TAILQ_REMOVE(&q->head, returned_item, entries);
    free(returned_item);
}

int main() {

    q_item q;
    TAILQ_INIT(&q.head);

    enqueue(1, &q);
    enqueue(2, &q);
    enqueue(3, &q);

    dequeue(&q);
    dequeue(&q);

    return 0;
}

另外,查看此宏的扩展方式可能很有用,只需使用gcc选项使用-E进行编译,您将看到例如TAILQ_HEAD(, qitem) head扩展为

struct {
       struct q_item *tqh_first; 
       struct q_item **tqh_last; 
 } head;

您还可以在<sys/queue.h> header

中找到它
/*
 * Tail queue definitions.
 */
#define TAILQ_HEAD(name, type)                                  \
struct name {                                                   \
    struct type *tqh_first; /* first element */                 \
    struct type **tqh_last; /* addr of last next element */     \
}

这就是为什么您尝试使用void enqueue(int n, TAILQ_HEAD(, q_item) * head)无效的原因,因为预处理程序可以简单地替换。