C语言 为什么我收到“警告:来自不兼容指针类型的赋值”?结构数组中的双链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4038621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am i getting "warning: assignment from incompatible pointer type"? Double linked list in a struct array
提问by FILIaS
I'm trying to make an implementation of a double linked list which connects to an array. The struct that makes the array contains the list's Head and Tail pointer.
我正在尝试实现连接到数组的双链表。构成数组的结构包含列表的 Head 和 Tail 指针。
typedef struct myStruct{
int code;
struct myStruct *Head;
struct myStruct *Tail;
}myStruct;
myStruct MyArray[10];
Here's my double linked list:
这是我的双链表:
struct myList
{
int data;
struct myList *previous;
struct myList *next;
}head;
struct myList *start =NULL;
On the following piece of code im getting the warning which i wrote on the title of the post
在下面的一段代码中,我收到了我在帖子标题上写的警告
warning: assignment from incompatible pointer type
警告:从不兼容的指针类型赋值
void add_neighbor_to_neighborList(struct neighborList *start,int code,int Data)
{
struct myList *newNode = (struct myList *) malloc (sizeof(struct myList )); /* creates a new node of the correct data size */
newNode->data = Data;
if (start == NULL) { /*WARNING!checks to see if the pointer points somewhere in space*/
MyArray[code].Head=newNode; //WARNING
newNode->previous=MyArray[code].Head;
}
else
{
MyArray[code].Tail->next=newNode; /*error: ‘struct myStruct' has no member named ‘next'*/
newNode->previous=MyArray[code].Tail; //WARNING
}
/*if (newNode == NULL){
return NULL;
}*/
MyArray[code].Tail=newNode; //WARNING
newNode->next=MyArray[code].Tail; //WARNING
//return *start;
}
I'm looking at it so much time but still cant find whats wrong and what i should correct. If you had any idea,I would appreciate that! Thanks anyway/in advance! ;)
我看了这么多时间,但仍然找不到什么是错的,我应该纠正什么。如果您有任何想法,我将不胜感激!无论如何/提前谢谢!;)
回答by Bertrand Marron
MyArray[code].Head=newNode; //WARNING
MyArray[code].Head's type is struct myStruct *.
MyArray[code].Head的类型是struct myStruct *.
newNode's type is struct myList *.
newNode的类型是struct myList *.
struct myStruct *and struct myList *are pointers to two different types, they are incompatible, that's what your compiler is telling you.
struct myStruct *并且struct myList *是指向两种不同类型的指针,它们不兼容,这就是您的编译器告诉您的。
You probably wanted your struct myStructto be defined like this:
你可能希望你的struct myStruct定义是这样的:
typedef struct myStruct{
int code;
struct myList *Head;
struct myList *Tail;
} myStruct;
I'd suggest you to use more explicit type names so errors like that wouldn't happen.
我建议您使用更明确的类型名称,这样就不会发生这样的错误。
You could rename your struct myStructto struct myListand rename struct myListto struct myNode. Because it's what they are, or seem to be.
您可以将您的重命名struct myStruct为struct myList和重命名struct myList为struct myNode. 因为这就是它们的样子,或者看起来是这样。
回答by Jeff Mercado
You have two different types, myStructand struct myList. They are not the same type (though they contain the same kind of types in the same order). Use one or the other.
您有两种不同的类型,myStruct并且struct myList. 它们不是相同的类型(尽管它们以相同的顺序包含相同类型的类型)。使用其中之一。
The array, MyArrayis of myStructs which contains pointers to struct myStruct. You are attempting to assign a pointer to a struct myListto those pointers. They are not the same hence the warning.
数组MyArray是myStructs ,它包含指向 的指针struct myStruct。您正在尝试将指向 a 的指针分配struct myList给这些指针。它们不一样,因此警告。
回答by EboMike
You're trying to assign a myList pointer to something that expects a myStruct pointer. You can't do that.
您正在尝试将 myList 指针分配给需要 myStruct 指针的内容。你不能那样做。
A* pA;
B *pB;
pB = pA; // Can't do this without casting

