[英]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 是区分大小写的,所以
Node
和NODE
是两个不同的、不相关的东西。 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 Node
和Node
不是相同的类型。
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.