I\'m trying to make a function that compares nodes in alphabetical order in C pr
ID: 3705161 • Letter: I
Question
I'm trying to make a function that compares nodes in alphabetical order in C programming , but my code keeps throwing an eception at the first strcmp() stating curr_node was nullptr. How do I fix this?
void insert_alphabetically(Student_t **start, Student_t *new_node)
{
Student_t *curr_node = *start;
if (curr_node = NULL)
{
*start = new_node;
}
if (strcmp(curr_node->name, new_node->name) > 0) //something wrong right here ....... curr_node was nullptr.
{
new_node->next = curr_node;
*start = new_node;
}
while (curr_node->next != NULL)
{
curr_node = curr_node->next;
}
if (strcmp(curr_node->name, new_node->name) < 0)
{
curr_node->next = new_node;
}
else
{
curr_node = *start;
while (strcmp(curr_node->name, new_node->name) < 0)
{
curr_node = curr_node->next;
}
new_node->next = curr_node->next;
curr_node->next = new_node;
}
}
Explanation / Answer
First if condition should have 2 equals
If(curr_node==NULL) //if your are checking null. Single equal sign will work if you are assigning your null values to curr_node.