Page 1 of 1

pb pointer type

Posted: Thu Jan 13, 2005 10:26 am
by thund
Hello everybody.
I have write a program on the linkedlist but when I want to compile its, there are lots of same error. This is errors that I have :
linkedlist.c: In function `add_head':
linkedlist.c:19: warning: initialization from incompatible pointer type
linkedlist.c:20: warning: assignment from incompatible pointer type
linkedlist.c:23: warning: assignment from incompatible pointer type
linkedlist.c:24: warning: assignment from incompatible pointer type


And this is a part of my program :

typedef struct _node{
struct node *prev;
void *data;
struct node *next;
} node;

node* add_head(node *list_node, void* d){
node *n;
node *p=list_node->prev;
p->next=n;
n->prev=list_node->prev;
n->data=d;
n->next=list_node;
list_node->prev=n;
return n;
}

Help me please. It's very important.
Thank's.

Re:pb pointer type

Posted: Thu Jan 13, 2005 2:14 pm
by proxy

Code: Select all

typedef struct _node{
  struct node *prev;
  void *data;
  struct node *next;
} node;
you have an error here (dunno if it's related)

anyway, "node" is a typdef and "_node" is a formal struct
so you shoudl be doing this:

Code: Select all

typedef struct _node{
  struct _node *prev;
  void *data;
  struct _node *next;
} node;
BTW why put the data before "next" it is usually best to put the prev/next entries before everything cause this way you can have your traversal code deal with structs like this:

Code: Select all

typedef struct _node_entry {
  struct _node_entry *prev;
  struct _node_entry *next;
} node_entry;
with some casting and it doesn't need to know about what type data is...but that's a different story :-P

proxy

Re:pb pointer type

Posted: Thu Jan 13, 2005 2:15 pm
by IRBMe
typedef struct node{
node *prev;
void *data;
node *next;
} node;

try just that.

But I've no idea what you're code is doing. I mean, the variable n is left uninitialized and never assigned anything for a start. You just declare it, leave it uninitialized, then start trying to write to whatever it points to (n->prev=...). That's going to cause alot of crashes if it ever even manages to compile.

Re:pb pointer type

Posted: Fri Jan 14, 2005 2:58 am
by Pype.Clicker
IRBMe wrote: typedef struct node{
node *prev;
void *data;
node *next;
} node;

try just that.
that won't compile either. The compiler will insist that "node" hasn't been declared prior it is used... I mean, when the internals of "struct node" are declared, the compiler doesn't know a bit about "struct node" itself ... If you tell it to insert a field of type "struct node*", it's okay because the compiler just need to know it's a pointer type and that it refers to a structure called "node" which will be declared later.

Code: Select all

typedef struct node {
   struct node* prev;
   struct node* next;
   void *data;
} node;
is perfectly correct code. (e.g. the compiler now knows that "node" is an alias for "struct node". Both can coexist, no underscore needed)

Code: Select all

typedef struct node node;

struct node {
   node* prev;
   node* next;
   void* data;
};
should be valid aswell. Declaring (without defining) structures ahead is a perfectly valid solution, and it's *the* only solution when you have cross-referencing structures like

Code: Select all


struct directory;
struct file;

struct directory {
   struct directory* parent;
   struct file* files;
   whatever;
};

struct file {
    struct directory *parent;
    whatever;
};
Now, this is *really* general programming, so i'm moving it.