0

プラットホーム:

Ubuntu 12.04 amd64、gcc、arm-linux-gnueabi-gcc

使用したライブラリ:

FreeBSD ライブラリ

質問:

私は FreeBSD ライブラリを勉強しています。(参考

を使用したときにコンパイルエラーが発生しましたLIST_FOREACH_SAFE。バグを修正する方法がわかりませんでした。

間違った出力:

test.c:39: error: ‘entries’ undeclared (first use in this function)
test.c:39: error: (Each undeclared identifier is reported only once
test.c:39: error: for each function it appears in.)
test.c:39: error: expected ‘;’ before ‘{’ token

コード:

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

int main()
{
    LIST_HEAD(listhead, entry) head =
        LIST_HEAD_INITIALIZER(head);
    struct listhead *headp;                 /* List head. */
    struct entry {
        int a;
        LIST_ENTRY(entry) entries;      /* List. */
    } *n1, *n2, *n3, *np, *np_temp;

    LIST_INIT(&head);                       /* Initialize the list. */

    n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
    n1->a = 7;
    LIST_INSERT_HEAD(&head, n1, entries);

    n2 = malloc(sizeof(struct entry));      /* Insert after. */
    n2->a = 5;
    LIST_INSERT_AFTER(n1, n2, entries);

    n3 = malloc(sizeof(struct entry));      /* Insert before. */
    n3->a = 1;
    LIST_INSERT_BEFORE(n2, n3, entries);

    LIST_REMOVE(n2, entries);               /* Deletion. */
    free(n2);

    /* Forward traversal. */
    LIST_FOREACH(np, &head, entries) {
        printf("%d\n", np->a);
    }


    /* Safe forward traversal. */
    LIST_FOREACH_SAFE(np, &head, entries, np_temp) {
        // do somethings
        LIST_REMOVE(np, entries);
        free(np);
    }

    return 0;
}
4

1 に答える 1