Passing a semicolon or comma as a Macro argument

Discussion in 'C Programming' started by MattyWix, Sep 7, 2006.

  1. MattyWix

    MattyWix Guest

    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
    MattyWix, Sep 7, 2006
    #1
    1. Advertising

  2. MattyWix

    Chris Dollin Guest

    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.
    Chris Dollin, Sep 7, 2006
    #2
    1. Advertising

  3. MattyWix

    Walter Banks Guest

    #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, Sep 7, 2006
    #3
  4. Konstantin Miller <> wrote:

    > > #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.
    Christopher Benson-Manica, Sep 7, 2006
    #4
  5. 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, Sep 7, 2006
    #5
  6. MattyWix

    Thad Smith Guest

    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, Sep 11, 2006
    #6
  7. MattyWix

    Chris Dollin Guest

    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, Sep 11, 2006
    #7
  8. MattyWix

    Thad Smith Guest

    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
    Thad Smith, Sep 17, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan
    Replies:
    2
    Views:
    501
    Francois Grieu
    Jul 5, 2004
  2. pedroalves
    Replies:
    6
    Views:
    418
    Peter Nilsson
    Sep 19, 2006
  3. dmitrey
    Replies:
    4
    Views:
    450
  4. Dave Slayton
    Replies:
    1
    Views:
    183
    Ben Morrow
    Mar 11, 2007
  5. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    210
    Michael Winter
    Dec 11, 2006
Loading...

Share This Page