468,457 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Passing a semicolon or comma as a Macro argument

Hi,

How can I pass a semicolon or a comma as a macro argument.
I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.

MattyWix

Sep 7 '06 #1
7 10859
MattyWix wrote:
How can I pass a semicolon or a comma as a macro argument.
I don't think you can. (I don't think you can switch off the
argument-separating function of the comma.) There might be
deferred-macro-expansion tricks you can pull. Someone else
can propose those: I get twitchy with clever macro tricks.
I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.
Well, I have three\\\\\four suggestions (likely there are more
possibilities):

(a) Have two macros. Share common text as other macros.

(b) Use a non-C macro preprocessor.

(c) Generate the code, don't macro-process it.

(d) Back off and review the problem and see if there's a
completely different way of solving it.

Looking at the code you've shown us, I'd go for (d).

--
Chris "seeker" Dollin
The shortcuts are all full of people using them.

Sep 7 '06 #2
#define comma ,
pass comma where you need a comma
MattyWix wrote:
Hi,

How can I pass a semicolon or a comma as a macro argument.
I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.

MattyWix
Sep 7 '06 #3
Walter Banks wrote:
#define comma ,
pass comma where you need a comma
Can you make any compilable example?
>
MattyWix wrote:
>Hi,

How can I pass a semicolon or a comma as a macro argument.
I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.

MattyWix
Konstantin
Sep 7 '06 #4
Konstantin Miller <sl****@ournet.dewrote:
#define comma ,
pass comma where you need a comma
Can you make any compilable example?
Sure:

#define comma ,
#define invoke(f,args) f(args)

#include <stdio.h>

int main(void)
{
invoke( printf, "Hello, world! %d, %d, %d\n" comma 1 comma 2 comma 3 );
return 0;
}

It should be obvious, however, that this is obfuscatory and thus a Bad
Idea. OP would be advised to find a different solution.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sep 7 '06 #5
Chris Dollin wrote:
MattyWix wrote:
>>I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.
(a) Have two macros. Share common text as other macros.
I like that approach:

#define LIST ITEM(a),ITEM(b),ITEM(c)

#define ITEM(x) x,
enum e {
LIST,
NUM_ENTRIES
};

#undef ITEM
#define ITEM(x) x;
struct s {
LIST
};

--
Thad
Sep 11 '06 #6
Thad Smith wrote:
Chris Dollin wrote:
>MattyWix wrote:
>>>I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.
>(a) Have two macros. Share common text as other macros.

I like that approach:

#define LIST ITEM(a),ITEM(b),ITEM(c)

#define ITEM(x) x,
enum e {
LIST,
NUM_ENTRIES
};

#undef ITEM
#define ITEM(x) x;
struct s {
LIST
};
I don't like that implementation of it.

--
Chris "seeker" Dollin
The "good old days" used to be much better.

Sep 11 '06 #7
Chris Dollin wrote:
Thad Smith wrote:
>>Chris Dollin wrote:
>>>MattyWix wrote:
I wish to build an expression that in some cases has a comma - eg
building a list of members for a structure, but in other instances has
a comma - eg building a list of arguments for a function.
>>>(a) Have two macros. Share common text as other macros.

I like that approach:

#define LIST ITEM(a),ITEM(b),ITEM(c)

#define ITEM(x) x,
enum e {
LIST,
NUM_ENTRIES
};

#undef ITEM
#define ITEM(x) x;
struct s {
LIST
};

I don't like that implementation of it.
It's a little messy, but the best I can think of to have multiple uses
of a single list using only C. You could do something nicer with an
external preprocessor. Feel free to contribute your own solution.

--
Thad
Sep 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grey Plastic | last post: by
11 posts views Thread by Shawn Odekirk | last post: by
14 posts views Thread by Michael B Allen | last post: by
2 posts views Thread by grid | last post: by
20 posts views Thread by jason | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.