简体   繁体   English

不兼容的指针类型结构节点到节点

[英]Incompatible pointer types struct node to node

I'm doing a lab for my course and I'm having an issue with some warnings.我正在为我的课程做一个实验,但我遇到了一些警告问题。 We are given a struct that we cannot change:我们得到了一个无法改变的结构体:

typedef struct Element{
    int value;
    double key1;
    double key2;
    struct NODE* next;
    struct NODE* sort1;
    struct NODE* sort2;
}Node;

And this is the main code:这是主要代码:

struct Element * randomizer(int A){
     /*Function to have "real" random*/

    srand((unsigned int) time(0));

     Node *head;
     Node *ptr;
    int i = 0;

    head = NULL;
    ptr = NULL;



    /*Vlaues beiong intialized*/
    for ( i = 0 ; i <= A; i++){
    ptr = malloc(sizeof(Node));
        ptr->value = rand()%11;
        while (ptr->value == 0 || ptr->value == 10 ){
            ptr->value = rand()%11;
        }
    ptr->key1 =  (rand()%41)+10;

        while (ptr->value == 10.0 || ptr->value == 50.0 ){
              ptr->key1 =  (rand()%41)+10;
        }
    ptr->key2 = (rand()%41)+50;

        while (ptr->value == 50.0 || ptr->value == 90.0 ){
                     ptr->key2 =  (rand()%41)+50;
               }

    ptr->next = head;

    head = ptr;
    }

    ptr->sort1 = NULL;
    ptr->sort2 = NULL;

    return ptr;
}

At ptr->next = head;ptr->next = head; I get an error saying我收到一条错误消息

incompatible pointer types assigning type node to struct node不兼容的指针类型将类型节点分配给结构节点

How can I do this correctly?我怎样才能正确地做到这一点?

head is declared as Node * , and because of the typedef this is equivalent to struct Element* . head被声明为Node * ,并且由于 typedef 这相当于struct Element*

The next member is declared as struct NODE * . next成员被声明为struct NODE *

struct NODE * is not the same as struct Element * . struct NODE *struct Element *

You probably should change the struct member to您可能应该将结构成员更改为

struct Element *next;

C is case-sensitive, so Node and NODE are two different, unrelated things. C 是区分大小写的,所以NodeNODE是两个不同的、不相关的东西。 You're trying to use Node in your code, but the declaration you say you can't change has NODE , so they are incompatible.您正在尝试在代码中使用Node ,但您说无法更改的声明具有NODE ,因此它们不兼容。 You probably need to change your code to also use NODE .您可能需要更改代码以也使用NODE

In this typedef declaration在这个 typedef 声明中

typedef struct Element{
    int value;
    double key1;
    double key2;
    struct NODE* next;
    struct NODE* sort1;
    struct NODE* sort2;
}Node;

You declared two different structures.您声明了两个不同的结构。 The first one has the type struct Element with the type alias Node and within its definition you declared the type struct NODE .第一个具有类型别名 Node 的struct Element类型,并在其定义中声明了类型struct NODE

Even if there was not a typo and the data members have the type struct Node * instead of struct NODE * nevertheless the types struct Node and Node are not the same types.即使没有拼写错误并且数据成员的类型为struct Node *而不是struct NODE *但是类型struct NodeNode不是相同的类型。

You should declare the structure like你应该像这样声明结构

typedef struct Element{
    int value;
    double key1;
    double key2;
    struct Element* next;
    struct Element* sort1;
    struct Element* sort2;
}Node;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:不兼容的指针类型从“ mode_t”(又称为“ unsigned int *”)分配给“ node_t *”(也称为“结构节点*”) - Warning: Incompatible pointer types assigning to 'node_t *' (aka 'struct node *') from 'mode_t' (aka 'unsigned int *') 分配给结构的不兼容指针类型 - Incompatible pointer types assigning to struct 初始化基本Trie节点的不兼容指针类型 - incompatible pointer types initializing initializing a basic trie node 从链表中的“node *”分配给“int *”的不兼容指针类型 - Incompatible pointer types assigning to 'int *' from 'node *' in linked list C 从类型“struct Node *”分配到类型“struct Node”时类型不兼容 - C incompatible types when assigning to type 'struct Node' from type 'struct Node *' 从类型“struct node”分配到类型“struct node *”时的类型不兼容 - Incompatible types when assigning to type 'struct node *' from type 'struct node' 与typedef和struct不兼容的指针类型警告 - Incompatible pointer types warning with typedef and struct 节点ffi指向结构的指针 - Node ffi pointer to struct 结构节点下一个指针 - Struct node next pointer 在结构中引用结构会生成“不兼容指针类型”警告 - Referencing struct within a struct generates “incompatible-pointer-types” warnings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM