Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   saving a #define before an #undef (http://www.velocityreviews.com/forums/t596081-saving-a-define-before-an-undef.html)

myheartinamerica 03-05-2008 12:08 AM

saving a #define before an #undef
 
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.

Peter Nilsson 03-05-2008 12:21 AM

Re: saving a #define before an #undef
 
myheartinamerica <myheartinamer...@gmail.com> 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

myheartinamerica 03-05-2008 12:46 AM

Re: saving a #define before an #undef
 
> > #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 ****ing face off every time I go
to work.

Thanks again for the preprocessor help, though.

-Mick


All times are GMT. The time now is 05:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.