On Monday, July 1, 2024 at 11:03:33 PM UTC-6 Antoine Riard wrote:
Here a snippet example of linked list code checking the pointer (`*begin_list`) is non null before the comparison operation to find the target element list.
```
pointer_t ft_list_find(pointer_t **start_list, void *data_ref, int (*cmp)())
{
while (*start_list)
{
if (cmp((*start_list)->data, data_ref) == 0)
return (*start_list);
*start_list = (*start_list)->next;
}
return (0);
}
```
I assume this function lets you search for an element starting in the middle of a single-linked list (the middle because you could call `ft_list_find(&p-next, data_ref)` where `p` points to any element in the middle of the list, including possibly the last item in the list, in which case the loop body wouldn't run). If so, I don't think this does what's intended. This actually unlinks (and memory-leaks) elements up to where the match is found. I think you want to advance `start_list` this way (I didn't test this):
```
start_list = &(*start_list)->next;
```