Chuck Johnson <
galo...@gmail.com> writes:
> I keep getting the following warning errors when I compile with gcc.
> Any help would be GREATLY appreciated! Thanks!
>
<snip>
> ./Desktop/C Programs/HeadFirstTests.c: In function ‘addFirst’:
> ./Desktop/C Programs/HeadFirstTests.c:40:16: warning: assignment from incompatible pointer type [enabled by default]
<snip>
>
> Here is my code:
<snip>
>
> typedef struct { // Define a linked list node
> char *name;
> struct LLNODE *next;
> }LLNODE;
>
> LLNODE *head = NULL; //Defind global pointer variable head
I'm going to give another explanation, despite your having an answer
already...
The problem is that 'next' is defined to be a pointer to a 'struct
LLNODE' but the other pointer involved in the assignment, 'head', is of
a different type altogether: it's a pointer to an LLNODE.
The typedef makes LLNODE be an alias for an anonymous struct, whereas
the declaration of 'next' just informs the compiler that, somewhere,
there is another type called struct LLNODE about which more might be
forthcoming later -- that's called "completing the type". The only
complete struct here is the anonymous one inside the typedef.
In C, type names defined by typedef and "struct tags" (the names that
can come after the keyword struct) live in quite separate spaces and are
never assumed to be the same just because the are spelt the same. You
can, if you wish,
typedef struct A B;
typedef struct B A;
and you may then assign a struct A * to a B * variable (or, indeed,
assign a struct B * to an A * variable) but the compiler will complain
if you try to assign a struct B to a B *.
<snip>
--
Ben.