我正在执行一个 C 程序,但收到警告:
警告:来自不兼容指针类型的赋值
我在这里复制相关代码:
//Structure I am using:
typedef struct graph_node
{
int id;
int weight;
struct node *next;
}node, dummy;
node *head[10];
// Function which is generating this warning:
void print_list()
{
int i;
for (i = 0;i< vertex; i++)
{
printf ("\n ==>>%d ", head[i]->id);
while (head[i]->next != NULL)
{
head[i] = head[i]->next;
printf ("\t ==>>%d ", head[i]->id); /******This line is generating warning ********/
}
}
}
上面的代码可以正常编译并抛出以下警告:
警告:从不兼容的指针类型为链接列表数组赋值
你不应该写struct node *next;
as node
is not typedef
还没有和struct node
根本不存在。
您应该将您的结构重新声明为:
typedef struct graph_node
{
int id;
int weight;
struct graph_node *next;
/* ^^^^^^ */
}node, dummy;
为什么我的代码可以编译
当你只写struct node *next;
,你的编译器假设struct node
是不完整类型(仅声明)并允许指向该类型的指针。
当你转换类型的指针时struct node
to node
(这是一个typedef
of struct graph_node
),会出现不兼容的指针转换警告,以警告您任何严格的别名规则破坏或类似的其他问题。
假设struct node
成为不完整的类型有点宽泛,是一个单独的问题。
是的,正在为线路发出警告head[i] = head[i]->next;
而不是下一个:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)