1

I keep getting an "Assignment from incompatible pointer type" and I can't figure out why. I thought it looked right. I'm just trying to do the basics of a linked list in C.

typedef struct{
   int id;
   struct node *next;
} node;

node *root = NULL; // Sets up a root node when the program starts.

create nodes(int id){
   if(root == NULL){
      root = (node*)malloc(sizeof(node));
      root-> id = -1;
      node *nextNode;
      nextNode = (node*)malloc(sizeof(node));
      nextNode -> id = id;
      root-> next = nextNode; // This line is throwing an error.
   }
}

I feel like it's something simple but I can't put my finger on it...

DragonVet
  • 399
  • 2
  • 6
  • 19
  • 2
    That doesn't look like a valid function definition. Do you have any crazy macros going? – Antimony Mar 09 '13 at 03:04
  • 1
    If you have a question about a compilation please provide a more complete program we can run (basically just putting the create_nodes directly inside the main would almost be enough) – hugomg Mar 09 '13 at 03:08
  • `create nodes` is a typo? – leonbloy Mar 09 '13 at 03:08
  • 5
    Who taught you to explicitly cast a result of `malloc`? Tell them they should stop doing that shit. –  Mar 09 '13 at 03:24
  • @VladLazarenko A certain very popular proprietary compiler only supports C89 and C++, so many people are forced to make their C compile as C++ in order to support said compiler. I agree that it sucks. – James M Mar 09 '13 at 15:01

3 Answers3

5

Your struct is actually an unnamed struct typedef-d to node, but you're trying to refer to it as struct node later (which is not the same as your node typedef). Quick fix is to simply give the struct a name:

typedef struct node {
   int id;
   struct node *next;
} node;

Or, if you prefer (and this is completely stylistic), remove the typedef and correct your other references to the struct:

struct node {
   int id;
   struct node *next;
};

struct node *root = NULL;

create nodes(int id){
   if(root == NULL){
      root = malloc(sizeof(struct node));
      root->id = -1;
      struct node *nextNode;
      nextNode = malloc(sizeof(struct node));
      nextNode->id = id;
      root->next = nextNode;
   }
}
James M
  • 18,506
  • 3
  • 48
  • 56
2

Four points here:

First. Add a name of the struct node, if you must include pointer in the field of the struct (as @JamesMcLaughlin pointed out above). For example:

typedef struct nodetag { int id; struct nodetag *next; } node;

Second. Make sure you are using the variable type create as intended. I assume user-defined variable type create exist within your #define or somewhere else. If not, this will cause a compiler error. Even if you did, this won't compile as you don't have a return statement that returns create.

Third. Include node *root = NULL; within your function nodes. Otherwise, function nodes won't recognize the variable root and results in a compiler error.

Fourth. Declare local variables in the beginning of the function. The line struct node *nextNode; will cause a compiler error for C89 as C89 does not allow type-declaration after statements. C99, however, allows such practice. It's advised to declare all of the local variables in the beginning of the function to be compatible with both C89 and C99.

Community
  • 1
  • 1
melvynkim
  • 1,655
  • 3
  • 25
  • 38
  • Writing modern C code to compile as C89 is a pretty horrible idea. What were you doing in 1989? :-) – James M Mar 09 '13 at 15:14
  • 1
    @JamesMcLaughlin Sadly, there still exist developers maintaining old codes or working on C89-only-compatible environments (e.g. MSVC). Although highly unlikely, I was unable to safely assume, based on the the information given in the question above, that the example code is being compiled on C99. – melvynkim Mar 10 '13 at 01:11
  • I was referring to your fourth point in which you advise the OP to write C89 rather than C99, which I consider very bad advice. We should encourage those using old compilers to upgrade, rather than altering our code to meet a standard which has been deprecated for 23 years. – James M Mar 10 '13 at 19:15
-1

Try this

struct node{
   int id;
   struct node *next;
} ;

struct node *root = NULL; // Sets up a root node when the program starts.

/* Return type is missing in your code*/ create_nodes(int id){
   if(root == NULL){
      root = (struct node*)malloc(sizeof(struct node));
      root-> id = -1;
      struct node *nextNode;
      nextNode = (struct node*)malloc(sizeof(struct node));
      nextNode -> id = id;
      root-> next = nextNode; // This line is throwing an error.
   }
}
uba
  • 2,012
  • 18
  • 21