Need help with c macro.

B

bob

Need help with c macro.

Have this call in my c program.

stub.c

SYMBOL(ALPHA) << no, dont want to change this to any other form.
SYMBOL(BETA)

Need a macro to expand each of above to this.

static struct somestruct struct_ALPHA = {"ALPHA"};
static struct somestruct struct_BETA = {"BETA"};
..
..

Cannot go beyond the following.

#define SYMBOL(ARG) \
static struct _CONCAT(struct_,ARG) = {?? what shud be here??};


Any help is appreciable.

Thx Bob.
 
A

Arthur J. O'Dwyer

SYMBOL(ALPHA) << no, dont want to change this to any other form.
SYMBOL(BETA)

Need a macro to expand each of above to this.

static struct somestruct struct_ALPHA = {"ALPHA"};
static struct somestruct struct_BETA = {"BETA"};

Okay, so just copy down what you *want* to happen, and then
replace the ALPHA bits with some dummy identifier, and wrap the
whole thing in a #define:

#define SYMBOL(foo) \
static struct somestruct struct_##foo = { #foo };

That's all.

HTH,
-Arthur
 
E

Eric Sosman

bob said:
Need help with c macro.

Have this call in my c program.

stub.c

SYMBOL(ALPHA) << no, dont want to change this to any other form.
SYMBOL(BETA)

Need a macro to expand each of above to this.

static struct somestruct struct_ALPHA = {"ALPHA"};
static struct somestruct struct_BETA = {"BETA"};
..
..

Cannot go beyond the following.

#define SYMBOL(ARG) \
static struct _CONCAT(struct_,ARG) = {?? what shud be here??};

#define SYMBOL(x) static struct somestruct CONCAT(struct_,x) = { #x };
#define CONCAT(x,y) x ## y

Note CONCAT instead of _CONCAT; to learn why, see

http://www.oakroadsystems.com/tech/c-predef.htm
Any help is appreciable.

ITYM "appreciated."
 
M

Martin Dickopp

Need help with c macro.

Have this call in my c program.

stub.c

SYMBOL(ALPHA) << no, dont want to change this to any other form.
SYMBOL(BETA)

Need a macro to expand each of above to this.

static struct somestruct struct_ALPHA = {"ALPHA"};
static struct somestruct struct_BETA = {"BETA"};
..
..

Cannot go beyond the following.

#define SYMBOL(ARG) \
static struct _CONCAT(struct_,ARG) = {?? what shud be here??};

#define CONCAT(a,b) a ## b
#define STRINGIFY(a) #a
#define SYMBOL(arg) \
static struct somestruct CONCAT (struct_, arg) = { STRINGIFY (arg) };

If you don't need the argument to SYMBOL expanded in case it is a macro
itself, you can use a slightly simpler macro:

#define SYMBOL(arg) \
static struct somestruct struct_ ## arg = { #arg };

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,138
Messages
2,570,804
Members
47,349
Latest member
jojonoy597

Latest Threads

Top