the compiler won't compile this because you have a potential error. The way to tell the compiler you know what you are doing is to change this
conductor->next = malloc(sizeof(struct node)); // errored
to
Code:
conductor->next = (node *)malloc(sizeof(struct node)); // errored
the (node *) say's that the data that is providing is a pointer to a node. This needs to be done to help prevent bugs in programs.
Similarly to the line
Code:
root = malloc(sizeof(struct root)); // 2 errors!
Looking at the code it doesn't seem to make sense. Here is how I changed it and tryed to make sense of it
Code:
// I changed the name to what it looks like the original code is trying to do
void add_node_to_end( struct node *root)
{
// struct node *root;
struct node *conductor;
// root = malloc(sizeof(struct root)); // really this line doesn't make sense interms of purpose
// root->next;
// root->x = 12; editing its own contents? I don't understand why
conductor = root;
if (conductor != 0)
{
while (conductor->next != 0)
{
conductor = conductor->next;
}
}
conductor->next = (node *)malloc(sizeof(struct node)); // errored
if (conductor == 0)
{
printf("Out of memory!");
return;
}
// origonally it allocats memory and then sets next to 0
// resulting in a memory leak
conductor = conductor->next
conductor->next = 0;
conductor->x = 0;
}
Hope this helps