I am currently trying to make a linked list and initialize it within a function. It looks like this:
void add_forest(node_t *head, unsigned char value)
{
int key;
node_t *current = head;
while (current->next != NULL)
{
current = current->next;
}
}
int main()
{
node_t *head;
*head = init_forest(); //error here
}
I am currently getting a segfault at the following area in my code and can not figure out why. I am creating the head in init_forest() and then passing back in the main. When I go through the init_forest() the tree does become built. Any suggestions?
current->next = head = (node_t *)malloc(sizeof(node_t));assigning toheadinadd_forest()is to blame.return *head;seems a little sketchy, since you change head incurrent->next = head = (node_t *)malloc(sizeof(node_t));. I take that back, the second time you enter add_forest, inwhile(current->next != NULL)you are dereferencing a likely null object (since you changed the contents of head in the previous call to add_forest). Maybe?