Danijel Danijel - 4 years ago 89
C Question

Token concatenation using ## doesn't work?

#define DECLARE_TYPE(T) \
typedef struct \
{ \
float value; \
int scale; \
} ae_T##_t;

DECLARE_TYPE(Q25);


This should create type
ae_Q25_t
, but it doesn't work.

How to rewrite?

AnT AnT
Answer Source

Firstly, naturally, it should be ae_##T##_t. (Why did you put that ## on only one side of T?)

Secondly, keep in mind that it won't work as expected if, say, Q25 is itself a macro and you want it substituted. To resolve this issue you need a two-tier macro definition

#define DECLARE_TYPE_(T)    \
    typedef struct          \
    {                       \
        float value;        \
        int scale;          \
    } ae_##T##_t;

#define DECLARE_TYPE(T) DECLARE_TYPE_(T)

But if you want DECLARE_TYPE(Q25) to always resolve to ae_Q25_t specifically, then you are good as is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download