开发者

Preprocessor data retrieval MACRO

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 NM开发者_StackOverflow社区SLIST_DATA(type,elem) (*((type*)((elem)->data)))
#define NMSLIST_DATA_REF(type,elem) ((type*)((elem)->data))


#define RETRIEVE(type, element) *((type*)((element)->data))

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

(untested, but it should work)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜