views:

56

answers:

2

I have the following problem:

"list.c"

struct nmlist_element_s {
    void *data;
    struct nmlist_element_s *next;
};

struct nmlist_s {
    nmlist_element *head;
    nmlist_element *tail;
    unsigned int size;
    void (*destructor)(void *data);
    int (*match)(const void *e1, const void *e2);
};

/*** Other code ***/

What will be the signature for a function that returns 'destructor' from the structure ? For example the signature of the function that returns 'size' is:

unsigned int nmlist_size(nmlist *list);

What will be the case for 'destructor' .

+7  A: 

This will work:

typedef void (*Destructor)(void *data);
Destructor getDestructor();
Richard Pennington
+3  A: 

General form:

void (*get_destructor())(void *data);

Exact form will depend on what parameters get_destructor is supposed to take. If you're just returning the destructor pointer from an instance of struct nmlist_s, then it will look like

void (*get_destructor(struct nmlist_s list))(void *data);
John Bode
Thanks, the syntax is a little strange, but this is what I was looking for.
Andrei Ciobanu
@nomemory: The syntax is _inductive_. It reads as: `get_destructor` is such that when applied some parameters (hence `get_destructor` is a function) it returns something on which `*` can be applied (the function returns a pointer). The result of that `*` is something on which a parameter of type `void *` can be applied (the pointer is a pointer to a function). Apply that parameter yields something of type `void`, i.e. nothing. To sum up: `get_destructor` is a function which takes some unspecified parameters and returns a pointer to a function which takes a `void *` and returns nothing.
Thomas Pornin
@Thomas Pornin , thanks for your clarification, now thing are clear.
Andrei Ciobanu