470,848 Members | 1,193 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

#redefine preprocessor macro statement.

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


Jun 27 '08 #1
3 18401
On Apr 14, 1:00*pm, Ernie.Pasv...@cggveritas.com wrote:
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.
Jun 27 '08 #2
On Apr 14, 4:00*pm, Ernie.Pasv...@cggveritas.com wrote:
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.
Jun 27 '08 #3
Kenneth Bull <ke**********@gmail.comwrites:
On Apr 14, 4:00*pm, Ernie.Pasv...@cggveritas.com wrote:
>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
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Charlie Zender | last post: by
3 posts views Thread by Avin Patel | last post: by
3 posts views Thread by songie D | last post: by
3 posts views Thread by Ernie.Pasveer | last post: by
3 posts views Thread by Default User | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.