#redefine preprocessor macro statement.

E

Ernie.Pasveer

Hi All,

Is there a way to create a macro called #redefine that will allow
redefinition without having to use #ifdef/#undef

For example, I'd like to do something like this :

#define THING "apple"
... code

#redefine THING "banana"
... code

#redefine THING "orange"
... code


Instead of :

#define THING "apple"
... code

#undef THING
#define THING "banana"
... code

#undef THING
#define THING "orange"
... code
 
U

user923005

Hi All,

Is there a way to create a macro called  #redefine that will allow
redefinition without having to use #ifdef/#undef

For example,  I'd like to do something like this :

    #define THING  "apple"
    ... code

    #redefine THING "banana"
    ... code

    #redefine THING "orange"
    ... code

Instead of :

    #define THING  "apple"
    ... code

   #undef THING
   #define THING "banana"
    ... code

   #undef THING
   #define THING "orange"
   ... code

void isolating_function(char *thing)
{
/* Do stuff here */
}


isolating_function("orange");
isolating_function("banana");
isolating_function("apple");

I think that it is good that you cannot do what you want to do. It is
trouble waiting to happen.
 
K

Kenneth Bull

Hi All,

Is there a way to create a macro called  #redefine that will allow
redefinition without having to use #ifdef/#undef

For example,  I'd like to do something like this :

    #define THING  "apple"
    ... code

    #redefine THING "banana"
    ... code

    #redefine THING "orange"
    ... code

just change the #redefine's to #define's

Instead of :

    #define THING  "apple"
    ... code

   #undef THING
   #define THING "banana"
    ... code

   #undef THING
   #define THING "orange"
   ... code



You don't need the #undef before reusing the #define.
 
K

Keith Thompson

No, there isn't. A macro expansion cannot include preprocessor
directives.
just change the #redefine's to #define's [...]
You don't need the #undef before reusing the #define.

Yes, you do. C99 6.10.3p2 says:

An identifier currently defined as an object-like macro shall not
be redefined by another #define preprocessing directive unless the
second definition is an object-like macro definition and the two
replacement lists are identical.

followed by similar wording for function-like macros.

I see this was cross-posted to comp.lang.c and comp.lang.c++. I
assume the rules for C++ are similar, but I'm not certain. It's
better to post to the newsgroup that covers the language you're
actually using.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top