I am executing a C program where I am getting warning: warning: assignment from incompatible pointer type
I am copying related code herein:
//Structure I am using:
typedef struct graph_node
{
int id;
int weight;
struct node *next;
}node, dummy;
node *head[10];
// Function which is generating this warning:
void print_list()
{
int i;
for (i = 0;i< vertex; i++)
{
printf ("\n ==>>%d ", head[i]->id);
while (head[i]->next != NULL)
{
head[i] = head[i]->next;
printf ("\t ==>>%d ", head[i]->id); /******This line is generating warning ********/
}
}
}
Above code compiles fine with throws the warning below:
warning: assignment from incompatible pointer type for linklist array
You should not write
struct node *next;asnodeis nottypedefed yet andstruct nodesimply does not exist.You should re-declare your structure as:
Why does my code compile then
When you write just
struct node *next;, your compiler assumesstruct nodeis an incomplete type (declaration only) and allows the pointer to this type.When you convert the pointer of type
struct nodetonode(which is atypedefofstruct graph_node), the incompatible pointer conversion warning occurs to warn you from any strict aliasing rule break or similar other issues.Is assuming
struct nodeto be an incomplete type is little broad and a separate question.And yes, the warning is being thrown for the line
head[i] = head[i]->next;and not the next one :)