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 12426
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: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |