C指针警告的隐式转换

Implicit conversion of C pointer warning

我使用的链表定义如下:

typedef struct {
    struct foo* next;
} foo;

假设已经设置了名为linked_list的头指针,我想遍历链表如下:

foo* curr = linked_list;
while(curr->next) {
    curr = curr->next;
}

我的编译器 (Clang) 发出有关从 struct foo* 转换为 foo* [-Wincompatible-pointer-types]

的警告

我知道我可以通过投射来抑制这个警告,但是有更好的方法吗?

问题是这个声明中使用的结构struct foo

typedef struct {
    struct foo* next;
    ^^^^^^^^^^
} foo;

从未定义。

作为此表达式语句的结果

curr = curr->next;

左操作数的类型为 foo *,而右操作数的类型为 struct foo *.

按以下方式重写结构体定义

typedef struct foo {
    struct foo* next;
} foo;