saving a #define before an #undef

Discussion in 'C Programming' started by myheartinamerica, Mar 5, 2008.

  1. Hello,

    I was working on a piece of code where a vendor's #define was causing
    compiler warnings, which led me to try and do the following:

    #if defined(VAR1)
    #define VAR1_SAVE VAR1
    #undef VAR1
    #endif
    #define VAR1 1
    #define VAR2 (10 - VAR1)
    #undef VAR1
    #if defined(VAR1_SAVE)
    #define VAR1 VAR1_SAVE
    #undef VAR1_SAVE
    #endif

    Even if this is ridiculous, I'm unsure as to why it didn't work. Any
    insights? I needed VAR2 to be #defined in terms of VAR1, but I needed
    it to be my own locally #define'd VAR1.
     
    myheartinamerica, Mar 5, 2008
    #1
    1. Advertising

  2. myheartinamerica <> wrote:
    > I was working on a piece of code where a vendor's #define
    > was causing compiler warnings, which led me to try and do
    > the following:
    >
    > #if defined(VAR1)
    >     #define VAR1_SAVE VAR1


    This will cause VAR1_SAVE to expand to, literally, VAR1.

    >     #undef VAR1


    This won't change what VAR1_SAVE will expand to.

    What it will do is remove any (further) substitution for
    VAR1.

    > #endif
    > #define VAR1 1
    > #define VAR2 (10 - VAR1)
    > #undef VAR1
    > #if defined(VAR1_SAVE)
    >     #define VAR1 VAR1_SAVE
    >     #undef VAR1_SAVE
    > #endif
    >
    > Even if this is ridiculous, I'm unsure as to why it
    > didn't work.


    Because macros are not 'variables'.

    > Any insights? I needed VAR2 to be #defined in terms of
    > VAR1, but I needed it to be my own locally #define'd VAR1.


    Either rename the third party VAR1 or change yours.

    As a style point, many people adopt naming conventions
    to mitigate (but not preclude) such namespace clash issues.

    --
    Peter
     
    Peter Nilsson, Mar 5, 2008
    #2
    1. Advertising

  3. > > #if defined(VAR1)
    > > #define VAR1_SAVE VAR1

    >
    > This will cause VAR1_SAVE to expand to, literally, VAR1.


    Much clearer now. Thanks. As far as naming conventions and style
    points go, I've found that commercial code leaves something to be
    desired. Basically I want to shoot my fucking face off every time I go
    to work.

    Thanks again for the preprocessor help, though.

    -Mick
     
    myheartinamerica, Mar 5, 2008
    #3
    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. Vikas Yadav
    Replies:
    0
    Views:
    1,143
    Vikas Yadav
    Sep 9, 2003
  2. Sachin

    #undef for function

    Sachin, Nov 25, 2005, in forum: C Programming
    Replies:
    3
    Views:
    431
    Peter Ammon
    Nov 28, 2005
  3. How to undef a typedef??

    , Nov 16, 2006, in forum: C Programming
    Replies:
    15
    Views:
    1,341
    Kenneth Brody
    Nov 18, 2006
  4. karthikbalaguru
    Replies:
    3
    Views:
    3,065
    Chris Dollin
    Nov 27, 2008
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    154
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page