平台:
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;
}