Reputation: 155
I am trying to create a linked list connected to other lists. The Main list will have index numbers and the sub lists will carry the data.
I have tried connecting a new node to the nodes of the main linked list but i am not sure if what i have done is correct. I am getting these errors during compilation :-
34:40: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
temp1->chain = temp2; ^
39:40: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
tempT2 = temp1; ^
#include <stdio.h>
#include <stdlib.h>
struct node1
{
int Numbering1;
int SizeOfList;
struct node1* link1;
struct node1* chain;
};
struct node2
{
int Numbering2;
int InList;
struct node2* link2;
};
struct node1* root = NULL;
void ListLink(int *count)
{
struct node1* temp1;
struct node2* temp2;
temp1 = (struct node1*)malloc(sizeof(struct node1));
temp2 = (struct node2*)malloc(sizeof(struct node2));
temp1->link1 = NULL;
temp1->chain = NULL;
temp2->link2 = NULL;
temp1->Numbering1 = *count;
*count++;
scanf("%d",&temp1->SizeOfList);
for(int i=0;i < temp1->SizeOfList; i++)
{
scanf("%d",&temp2->InList);
if(temp1->chain == NULL)
{
temp1->chain = temp2;
}
else
{
struct node2* tempT2;
tempT2 = temp1;
while(tempT2->link2 != NULL)
{
tempT2 = tempT2->link2;
}
tempT2->link2 = temp2;
}
}
if(root == NULL)
{
root = temp1;
}
else
{
struct node1* tempT1;
tempT1 = root;
while(tempT1->link1 != NULL);
{
tempT1 = tempT1->link1;
}
tempT1->link1 = temp1;
}
}
int main()
{
int index,query;
scanf("%d%d",&index,&query);
int OutList[index];
int count = 0;
ListLink(&count);
return 0;
}
I would like to know if I have made any mistakes in the program or if there are any better ways to do what i am trying to do.
Is there a way to typecast and remove that incompatible pointer type error
Upvotes: 0
Views: 66
Reputation: 44274
I'm not sure I fully understand what you are trying to do. The warnings (which you should treat as errors) are because you assing struct node1
pointers to struct node2
pointers (and vice versa).
My guess is that chain
is of wrong type. Change it like:
struct node1* chain; --> struct node2* chain;
^
note
Besides that - this line:
*count++;
is not doing what you expect - change it like:
*count++; --> (*count)++;
And here:
while(tempT1->link1 != NULL);
^
Sure you want this ; ?
It looks wrong...
I guess you need to delete the ;
Upvotes: 1
Reputation: 208
In the first warning, temp1->chain = temp2;, chain is a pointer of type "struct node1", and you are trying to assign temp2, its a pointer of type "struct node2". They are not compatible. viceversa in the second warning. you need to explicitly typecast when you assign to different datatypes. However in this case its not recommended as there will be possible truncation of data.
Upvotes: 0