警告:分配从兼容的指针类型的数组链表 [英] warning: assignment from incompatible pointer type for linklist array
问题描述
我执行我在哪里得到警告C程序:
警告:从兼容的指针类型赋值
我在此复制相关code:
//结构我使用:typedef结构graph_node
{
INT ID;
诠释权重;
结构节点*接下来的;
}节点,哑;节点*头[10];//功能是产生这样的警告:
无效print_list()
{
INT I; 对于(i = 0; I<顶点;我++)
{
的printf(\\ n ==>>%D,头[I] - > ID); 而(头[I] - >!下次= NULL)
{
头由[i] =头[Ⅰ] - >接着,
的printf(\\ t ==>>%D,头[I] - > ID); / ******这一行产生警告******** /
}
}
}
以上code编译罚款抛出下面的警告:
警告:从兼容的指针类型赋值为链表阵列
块引用>解决方案您不应该写
结构节点*旁边;
为节点
不是的typedef
ED又和结构节点
根本不存在。您应该重新申报的结构:
typedef结构graph_node
{
INT ID;
诠释权重;
结构graph_node *接下来的;
/ * * ^^^^^^ /
}节点,哑;为什么我的code编译然后
当你写的只是
结构节点*旁边;
,编译器假定结构节点
是一个不完整的类型(声明只),并允许该指针这种类型当你转换类型的指针
结构节点
到节点
(这是的typedef
的结构graph_node
),不兼容的指针转换警告时,从任何严格别名规则休息或类似的其他问题提出警告。时的假设
结构节点
是一个不完整的类型是小广泛而一个单独的问题。是的,警告被抛出的行
头[I] =头[I] - >接下来,
,而不是下一个:)I am executing a C program where I am getting warning: warning: assignment from incompatible pointer type
I am copying related code herein:
//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 ********/ } } }
Above code compiles fine with throws the warning below:
warning: assignment from incompatible pointer type for linklist array
解决方案You should not write
struct node *next;
asnode
is nottypedef
ed yet andstruct node
simply does not exist.You should re-declare your structure as:
typedef struct graph_node { int id; int weight; struct graph_node *next; /* ^^^^^^ */ }node, dummy;
Why does my code compile then
When you write just
struct node *next;
, your compiler assumesstruct node
is an incomplete type (declaration only) and allows the pointer to this type.When you convert the pointer of type
struct node
tonode
(which is atypedef
ofstruct graph_node
), the incompatible pointer conversion warning occurs to warn you from any strict aliasing rule break or similar other issues.Is assuming
struct node
to be an incomplete type is little broad and a separate question.And yes, the warning is being thrown for the line
head[i] = head[i]->next;
and not the next one :)这篇关于警告:分配从兼容的指针类型的数组链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!