views:

33

answers:

1

Hello, given the following structure:

struct nmslist_elem_s {
    nmptr data;
    struct nmslist_elem_s *next;
};
typedef struct nmslist_elem_s nmslist_elem;

Where:

typedef void* nmptr;

Is it possible to write a MACRO that retrieves the data from the element and cast it to the right type:

MACRO(type, element) that expands to *((type*)element->data). For example for int, i would need something like this: *((int*)(element->data)) .

Later edit: Yes they work, i was 'eating' some "(" and ")". This works:

#define NMSLIST_DATA(type,elem) (*((type*)((elem)->data)))
#define NMSLIST_DATA_REF(type,elem) ((type*)((elem)->data))
+2  A: 
#define RETRIEVE(type, element) *((type*)((element)->data))

RETRIEVE(int, nmptr)
// expands to
*((int*)((nmptr)->data))

(untested, but it should work)

James McNellis
Confirmed, this does work.
VeeArr