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. Advertisements

  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. Advertisements

  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. Advertisements

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. John Kandell
    Replies:
    4
    Views:
    4,449
    eeebop
    Dec 10, 2004
  2. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,753
    Luis Esteban Valencia
    Jan 6, 2005
  3. theotyflos
    Replies:
    3
    Views:
    702
    Thomas Matthews
    Feb 19, 2004
  4. robin liu
    Replies:
    3
    Views:
    1,102
    Robin Liu
    Apr 21, 2006
  5. karthikbalaguru
    Replies:
    3
    Views:
    3,436
    Chris Dollin
    Nov 27, 2008
  6. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    357
    Curt_C [MVP]
    Oct 3, 2003
  7. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    739
    Brian Takita
    Jan 23, 2006
  8. Tim McDaniel

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

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