By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,594 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

Need help with c macro.

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

On Tue, 3 Feb 2004, bob wrote:

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
Nov 14 '05 #2

P: n/a
fi*************@yahoo.com (bob) writes:
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
Nov 14 '05 #3

P: n/a
bob wrote:

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."

--
Er*********@sun.com
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.