views:

65

answers:

3

Hi, 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.

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

A: 

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.

Jeff M
hm maybe i wasnt clear (im gonna edit my post). myStruct is for the struct but i want another one for my double linked list (myList). All the elements of the array (MyArray) are of this struct myStruct. Each one should have a reference to its list. So i think i cant do it with only one struct :/
FILIaS
@FILIaS: Bertrand definitely has it then. You've defined the types switched so it is not what you had intended.
Jeff M
;) Thanx Jeff M anyway!
FILIaS
A: 

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
EboMike
+5  A: 
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.

Bertrand Marron
wow. i feel stupid! sth so simple and plain and i couldnt see it! Thank u very much Bertrand Marron! also thanx for the tip! ;)
FILIaS