Re: "warning: assignment from incompatible pointer type [enabled by default]"

2498 views
Skip to first unread message

LeJacq, Jean Pierre

unread,
Feb 26, 2013, 11:48:02 AM2/26/13
to
galo...@gmail.com wrote:

> Any help here would be GREATLY appreciated! Thanks!!!
>
> Here is my compiler response:
>
> "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] "
>
>
> /* 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;

You are attempting to declare next with a yet undefined type. You must first
make declare the struct.

/* Declaration of struct. */
struct LLNODE;

/* Declaration of typedef. */
typedef struct LLNODE LLNODE;

/* Definition of struct. */
struct LLNODE {
char *name;
struct LLNODE *next;
};

As a bonus tip, use of all upper case for names of structs is poor style.
These should be reserved for macros.

--
JP
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Marc Espie

unread,
Feb 26, 2013, 11:51:28 AM2/26/13
to
In article <clcm-2013...@plethora.net>, <galo...@gmail.com> wrote:
>
>typedef struct { // Define a linked list node
> char *name;
> struct LLNODE *next;
>}LLNODE;
>

you're confused about typedefs and unnamed struct.

This does two things:
- make the language aware that struct LLNODE should be a valid struct
(for which you have *no definition*, but this doesn't prevent the compiler
from declaring a pointer to it)
- typedef an anonymous struct as the name LLNODE.

So, struct LLNODE and LLNODE are totally different beasties.
No wonder the compiler warns about it.

I'm a bit surprised it just warns.

If you really want typedefs, you could use
typedef struct LLNODE {
char *name;
struct LLNODE *next;
} LLNODE;

but it doesn't bring you much.
Especially since you're obviously a beginner, I'd recommend spelling it
out for now, avoiding the typedef, an using the complete struct name
all the way...

Barry Schwarz

unread,
Feb 26, 2013, 11:47:59 AM2/26/13
to
On Fri, 4 Jan 2013 18:14:46 -0600 (CST), galo...@gmail.com wrote:

>Any help here would be GREATLY appreciated! Thanks!!!
>
>Here is my compiler response:
>
>"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]
>"
>
>
>/* 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

At this point, you have "forward declared" a tagless structure type.

> char *name;
> struct LLNODE *next;

At this point, you have "forward declared" a structure type called
struct LLNODE.

>}LLNODE;

At this point, you have finished the definition of the tagless
structure and created a type LLNODE that is an alias for this
structure. Note that LLNODE is a completely different type than
struct 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; //Keep getting errors here.

next is a pointer to struct LLNODE. head is a pointer to LLNODE. As
noted above, the two are not the same or even compatible types.

> head = newNode;
>
>}

The solution to your problem is to add the tag LLNODE to you tagless
structure so that LLNODE and struct LLNODE refer to the same type.

--
Remove del for email

Jasen Betts

unread,
Feb 26, 2013, 11:51:22 AM2/26/13
to
On 2013-01-05, galo...@gmail.com <galo...@gmail.com> wrote:
> Any help here would be GREATLY appreciated! Thanks!!!
>
> Here is my compiler response:

> ./Desktop/C Programs/HeadFirstTests.c:40:16: warning: assignment from incompatible pointer type [enabled by default]


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


Where is "struct LLNODE" defined ?

( the above defines "LLNODE" but does not define "struct LLNODE" )

--
⚂⚃ 100% natural

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Andrei Voropaev

unread,
Feb 26, 2013, 11:51:31 AM2/26/13
to
On 2013-01-05, galo...@gmail.com <galo...@gmail.com> wrote:
> warning: assignment from incompatible pointer type [enabled by default]
....
>
> typedef struct { // Define a linked list node
> char *name;
> struct LLNODE *next;
> }LLNODE;
...
>
> LLNODE *newNode;
> newNode = malloc(sizeof(LLNODE));
> newNode->name = data;
> newNode->next = head; //Keep getting errors here.
...

Of course it complains. You have never declared "struct LLNODE". So
compiler does not believe that assigning "LLNODE*" to "struct LLNODE*"
is acceptable. Standart way of handling this situation is

typedef struct LLNODE LLNODE;
struct LLNODE{
char * name;
LLNODE * next;
}

or

typedef struct LLNODE{
char *name;
struct LLNODE *next;
}LLNODE;

--
Minds, like parachutes, function best when open

Gordon Burditt

unread,
Feb 26, 2013, 11:51:36 AM2/26/13
to
> typedef struct { // Define a linked list node
> char *name;
> struct LLNODE *next;
> }LLNODE;

Where did you define a "struct LLNODE" (not LLNODE, *struct LLNODE*)?
Assigning a pointer to an LLNODE to a pointer to an undefined struct
type is going to draw the type of warning that you got.

If this is a C++ program, use a C++ compiler.
Reply all
Reply to author
Forward
0 new messages