3

I'm reading a book on algorithms and the author defines doubly linked list with this code:

void dlist_init(DList *list, void (*destroy)(void *data));

What is the use of function pointer to destroy function here? Can't we just later call the destroy() function on any list? Why pass pointer to it during initialization?

3
  • Presumably to encapsulate this information in one place. Commented Apr 2, 2013 at 20:06
  • Different types of data need different destroy functions, so you bundle the one to use with the list, I suspect. Commented Apr 2, 2013 at 20:06
  • Looks like Kyle Loudon's Mastering Algorithm with C... if so, that's explained. Commented Apr 2, 2013 at 21:34

1 Answer 1

7

The function pointer is passed to the initialization function so that the list functions will know how to destroy list entries. The list functions are designed to operate on all kinds of entries, so they need to be "told" how to destroy the particular entries this list will have.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.