警告:从linklist数组的不兼容指针类型分配

时间:2015-06-09 04:50:31

标签: c compiler-warnings incompatibletypeerror

我正在执行一个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  ********/
        }   
    }
}

上面的代码编译好了,抛出下面的警告:

  

警告:从linklist数组的不兼容指针类型分配

2 个答案:

答案 0 :(得分:2)

你不应该编写struct node * next;因为节点尚未定义,并且struct节点根本不存在。 您应该将结构重新声明为: typedef struct graph_node {     int id;     重量;     struct graph_node * next;     / * ^^^^^^ * / } node,dummy; 为什么我的代码会编译 当您只编写struct node * next;时,您的编译器假定struct node是一个不完整的类型(仅声明)并允许指向此类型的指针。 当您将struct node类型的指针转​​换为node(这是struct graph_node的typedef)时,会出现不兼容的指针转换警告,以警告您没有任何严格的别名规则中断或类似的其他问题。 假设struct node是一个不完整的类型是一个小问题,也是一个单独的问题。 是的,线头[i] = head [i] - &gt; next的警告被抛出;而不是下一个:)

答案 1 :(得分:0)

检查这段代码。我认为它可能会工作。并发布完整的代码,以便我可以解决完整的问题。

 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->next != NULL)
        {
            head[i] = head[i]->next;
         printf ("\t ==>>%d ", head[i]->id); 
               if(head[i]->next==NULL)
{
            printf ("\t ==>>%d ", head[i]->id); 

}

        }   
    }
}
相关问题