I implement a generic list, linked separately, in which list nodes store a pointer to their data.
typedef struct sll_node { void *data; struct sll_node *next; } sll_node;
To implement a universal find routine that works with any data, I wrote it so that it uses a function pointer for the comparison function as an argument, as follows:
/* eq() must take 2 arguments. ex: strcmp(char *, char *) */ sll_node *sll_find(void *data, int (*eq)(), sll_node *root);
You can pass the corresponding function pointer that works with the data type at hand. Therefore, if you store strings in list nodes, you can pass strcmp as an eq () function, and so on. It works, but I'm still not satisfied.
Is there a way to explicitly indicate the number of parameters of the comparison function without giving up its generality?
I tried this first:
sll_node *sll_find(void *data, int (*eq)(void *, void *), sll_node *root);
I expected this to work. But no (editing: compiled with a warning, but I have -Werror on!), I had to write a wrapper function around strcmp to match the eq prototype.
Then I tried:
sll_node *sll_find(void *data, int (*eq)(a, b), sll_node *root);
or
typedef int (*equality_fn)(a, b); sll_node *sll_find(void *data, equality_fn eq, sll_node *root);
which both will not compile since: "the list of parameters without types is allowed only in the definition of the function"