Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

warning: assignment from incompatible pointer type

129 views
Skip to first unread message

Chuck Johnson

unread,
Oct 12, 2012, 3:04:55 PM10/12/12
to
Hi All,

I keep getting the following warning errors when I compile with gcc. Any help would be GREATLY appreciated! Thanks!


gcc "./Desktop/C Programs/HeadFirstTests.c" -o "./Desktop/C Programs/HFT" && "./Desktop/C Programs/HFT"
./Desktop/C Programs/HeadFirstTests.c: In function ‘addFirst’:
./Desktop/C Programs/HeadFirstTests.c:40:16: warning: assignment from incompatible pointer type [enabled by default]
./Desktop/C Programs/HeadFirstTests.c: In function ‘printList’:
./Desktop/C Programs/HeadFirstTests.c:51:7: warning: assignment from incompatible pointer type [enabled by default]
Jerimiah
Robert
Johnson





Here is my code:

/* This program builds
a basic linked list.
gcc "./Desktop/C Programs/HeadFirstTests.c" -o "./Desktop/C Programs/HFT" && "./Desktop/C Programs/HFT"
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct { // Define a linked list node
char *name;
struct LLNODE *next;
}LLNODE;


void addFirst(char *data); //Declare function prototypes
void printList(void);

LLNODE *head = NULL; //Defind global pointer variable head


int main(int argc, char *argv[])
{

addFirst("Johnson");
addFirst("Robert");
addFirst("Jerimiah");
printList();


return EXIT_SUCCESS;
}

void addFirst(char *data)
{

LLNODE *newNode;
newNode = malloc(sizeof(LLNODE));
newNode->name = data;
newNode->next = head; //I keep getting errors on this line
head = newNode;

}

void printList(void)
{
LLNODE *itr = head;
while(itr != NULL)
{
printf("%s\n", itr->name);
itr = itr->next; //And errors on this line.
}
}

Ian Collins

unread,
Oct 12, 2012, 3:49:23 PM10/12/12
to
On 10/13/12 08:04, Chuck Johnson wrote:
> Hi All,
>
> I keep getting the following warning errors when I compile with gcc. Any help would be GREATLY appreciated! Thanks!
>
> gcc "./Desktop/C Programs/HeadFirstTests.c" -o "./Desktop/C Programs/HFT"&& "./Desktop/C Programs/HFT"
> ../Desktop/C Programs/HeadFirstTests.c: In function �addFirst�:
> ../Desktop/C Programs/HeadFirstTests.c:40:16: warning: assignment from incompatible pointer type [enabled by default]
> ../Desktop/C Programs/HeadFirstTests.c: In function �printList�:
> ../Desktop/C Programs/HeadFirstTests.c:51:7: warning: assignment from incompatible pointer type [enabled by default]
>
> Here is my code:
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
>
> typedef struct { // Define a linked list node
> char *name;
> struct LLNODE *next;

Here you have a problem with C's arcane declaration syntax.

"struct LLNODE" is interpreted as a forward declaration here, not as a
pointer to your new struct type because then name "LLNODE" hasn't been
introduced. You have to write either

typedef struct LLNODE { // Define a linked list node
char *name;
struct LLNODE *next;
} LLNODE;

or

struct LLNODE { // Define a linked list node
char *name;
struct LLNODE *next;
};

typedef struct LLNODE LLNODE;

to declare "struct LLNODE" before you reference it within its own
declaration.

--
Ian Collins

Keith Thompson

unread,
Oct 12, 2012, 4:34:02 PM10/12/12
to
Ian Collins <ian-...@hotmail.com> writes:
[...]
> Here you have a problem with C's arcane declaration syntax.
>
> "struct LLNODE" is interpreted as a forward declaration here, not as a
> pointer to your new struct type because then name "LLNODE" hasn't been
> introduced. You have to write either
>
> typedef struct LLNODE { // Define a linked list node
> char *name;
> struct LLNODE *next;
> } LLNODE;
>
> or
>
> struct LLNODE { // Define a linked list node
> char *name;
> struct LLNODE *next;
> };
>
> typedef struct LLNODE LLNODE;
>
> to declare "struct LLNODE" before you reference it within its own
> declaration.

Or you can drop the typedef and just use the struct tag:

struct llnode {
char *name;
struct llnode *next;
};

You'd then have to refer to the type as "struct llnode" rather than
just "LLNODE". (Note that since the identifier "llnode" will only
occur follwwing the keyword "struct", there's no need to make it
stand out by writing it in all-caps.)

There's nothing particularly wrong with using a typedef, but IMHO
it's not necessary.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Chuck Johnson

unread,
Oct 12, 2012, 5:34:26 PM10/12/12
to
Thanks! I can finally rest. :)

Ben Bacarisse

unread,
Oct 12, 2012, 5:40:43 PM10/12/12
to
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.
0 new messages