473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 12543
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Grey Plastic | last post by:
How do I pass text that contains a comma into a macro? For example, I have #define ATTRIBUTE(name,type) ... and I want to do ATTRIBUTE(transitions, map<int,int>); However, the last...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
2
by: s.subbarayan | last post by:
Dear all, What does this following piece of code do? #define CONVERT_SELF_PTR(type,in,out) \ (((in) != NULL) ? ((out) = (type *)(in), RETURN_OK) : ERROR_PARAM) especially can someone let...
14
by: Michael B Allen | last post by:
I just noticed that doing something like the following may fail because it can overwrite u->size before it's evaluated. memcpy(u, buf, u->size); Is it legit that this is a macro as opposed to...
2
by: grid | last post by:
Hi, I need some clarifications on how the comma operator is used to return values from function like macros.I saw a typical implementation as : #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 ,...
6
by: pedroalves | last post by:
Hi all, This is not a question about how to #define COMMA , Please keep reading. Recently in binutils, we introduced a macro like this: #define STRING_COMMA_LEN(STR) \ (STR), ((STR) ?...
2
by: slrj | last post by:
Can someone explain me what's going on in the code below. As per my knowledge "#define <macro name> <Macro replacement>" doesn't have a semicolon and it's terminated by a new line. But the code line...
5
by: moni | last post by:
Hi.. I am trying to use javascript for google maps display. If I call the javascript function from my aspx file I use: <input type="text" id="addresstext" value="Huntington Avenue,...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.