I'm having a problem with the tutorial on linked lists (Cprogramming.com Tutorial: Linked Lists).
When I try to build this code, I come up with an error.
The error is in lines 16 and 27, whenever malloc is used:Code:#include <stdio.h> #include <stdlib.h> struct node { int x; struct node *next; }; int main() { /* This won't change, or we would lose the list in memory */ struct node *root; /* This will point to each node as it traverses the list */ struct node *conductor; root = malloc( sizeof(struct node) ); root->next = 0; root->x = 12; conductor = root; if ( conductor != 0 ) { while ( conductor->next != 0) { conductor = conductor->next; } } /* Creates a node at the end of the list */ conductor->next = malloc( sizeof(struct node) ); conductor = conductor->next; if ( conductor == 0 ) { printf( "Out of memory" ); return 0; } /* initialize the new memory */ conductor->next = 0; conductor->x = 42; return 0; }
invalid conversion from `void*' to `node*'
I figured out that I could fix it by sticking (node*) in front of malloc. So my question is, should I have to do this? Is there something wrong with the tutorial or something wrong with the compiler? I'm using codeblocks 8.02 in windows xp with MinGW 5.1.4 (I'm using the latest stable versions of codeblocks and mingw)