472,972 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 472,972 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 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.

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...
2
isladogs
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...
0
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...
0
tracyyun
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...
2
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...
4
NeoPa
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 :...
3
NeoPa
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...
1
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...
3
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...
3
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...

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.