Macro preprocessing

Discussion in 'C Programming' started by dhun, Nov 7, 2009.

  1. dhun

    dhun Guest

    Hi All,
    I have a following kind of situation.

    #define i(p) p
    #define r(x) i(in)##_##x

    int main(){
    #define in a
    int a,a_b;
    a = r(b);
    #undef in
    }

    where I need to construct a symbol ab, at the place where code is
    executed.

    A g++ -E test.c, however insert a space between a and b.

    What could be possible solution for this?
     
    dhun, Nov 7, 2009
    #1
    1. Advertising

  2. dhun

    Andrey Vul Guest

    On Nov 7, 12:59 pm, dhun <> wrote:
    > Hi All,
    > I have a following kind of situation.
    >
    > #define i(p) p
    > #define r(x) i(in)##_##x
    >
    > int main(){
    > #define in a
    > int a,a_b;
    > a = r(b);
    > #undef in
    >
    > }
    >
    > where I need to construct a symbol ab, at the place where code is
    > executed.
    >
    > A g++ -E test.c, however insert a space between a and b.
    >
    > What could be possible solution for this?


    Premature ## . Create helper macros to redirect the preprocessor until
    only the expanded token is pasted.
    In this case:
    #define s__(x,y) x##y
    #define s(x,y) s__(x,y)
    #define r(x) s(i(in),s(_,x))

    Two things happen here:
    1. r is defined as the result of a redirected paste macro.
    2. s creates redirection so that results of expansion instead of the
    raw tokens are pasted.

    The paste operator (##) has a higher precedence than expansion. so you
    need to create a paster and a wrapper to allow for expansion before
    pasting.
    With my modifications, gcc does not warn about invalid tokens.
    This warning needs to be taken very seriously though, it shows that
    your code is missing something.
     
    Andrey Vul, Nov 7, 2009
    #2
    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. Dead RAM
    Replies:
    20
    Views:
    1,167
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    600
    Jack Klein
    Sep 21, 2003
  3. Henrik Goldman

    Recursing macro preprocessing?

    Henrik Goldman, Oct 21, 2006, in forum: C++
    Replies:
    4
    Views:
    389
    Kaz Kylheku
    Oct 22, 2006
  4. Francois Grieu
    Replies:
    6
    Views:
    394
    Ben Bacarisse
    Jan 18, 2008
  5. entik

    macro preprocessing C51 x armcc

    entik, Mar 23, 2012, in forum: C Programming
    Replies:
    1
    Views:
    399
    entik
    Mar 23, 2012
Loading...

Share This Page