开发者

Why am i getting "warning: assignment from incompatible pointer type"? Double linked list in a struct array

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 p开发者_如何学运维ointer.

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! ;)


MyArray[code].Head=newNode; //WARNING

MyArray[code].Head's type is struct myStruct *.

newNode's type is struct myList *.

struct myStruct * and struct myList * are pointers to two different types, they are incompatible, that's what your compiler is telling you.


You probably wanted your struct myStruct to be defined like this:

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 myStruct to struct myList and rename struct myList to struct myNode. Because it's what they are, or seem to be.


You have two different types, myStruct and 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.

The array, MyArray is of myStructs which contains pointers to struct myStruct. You are attempting to assign a pointer to a struct myList to those pointers. They are not the same hence the warning.


You're trying to assign a myList pointer to something that expects a myStruct pointer. You can't do that.

A* pA;
B *pB;

pB = pA; // Can't do this without casting
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜