警告:分配从兼容的指针类型的数组链表 [英] warning: assignment from incompatible pointer type for linklist array

查看:238
本文介绍了警告:分配从兼容的指针类型的数组链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行我在哪里得到警告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; as node is not typedefed yet and struct 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 assumes struct node is an incomplete type (declaration only) and allows the pointer to this type.

When you convert the pointer of type struct node to node (which is a typedef of struct 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆