I have been struggling with the program below for a couple of days to figure it out:
void *delete_from_list(struct node** list, int n)
{
struct node *entry = *list;
while (entry) {
if (entry->value == n) {
*list = entry->next;
free(entry);
}
list = &entry->next;
entry = entry->next;
}
}
I understand up to the line free(entry);
. I, however, can't grasp the last two lines. From my understanding, with this line list = &entry->next
the address of entry->next is assigned to a double pointer list
, and with the next line entry
points to the next node. But if free(entry)
releases the block of memory that entry
points to, it seems to me that entry
wouldn't point anywhere. Hence, entry->next
would appear to point nowhere, either. If so, the line entry = entry->next;
wouldn't make sense to me.