Question

I've been trying to get one pointer to equal the other pointer for quite some time but It just won't do it, and i'm not really sure why.

The structs are:

typedef struct{
    struct listNode* next;
} listNode;

typedef struct{
     listNode* head;
} linkedList;

However in code, when I try to execute: node->next = list->head

I get a 'assignment from incompatible pointer type' error. Any Help would be much appreciated since I can really see anything wrong! Thanks

Was it helpful?

Solution

You mention a struct listNode in the upper typedef, but that's not declared.

You need to do things in the right order:

               +----At this point, we can refer to "struct listNode"
               v 
struct listNode {
  struct listNode *next;  /* We know this self-references, uses the same name. */
};

/* Now establish a shorthand name for the struct ... */
typedef struct listNode listNode;

/* ... which we can use in subsequent declarations like this. */
typedef struct {
  listNode *head;
} linkedList;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top