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 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.
#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
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
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.
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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 ,...
|
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) ?...
|
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...
|
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,...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |