tags:

views:

102

answers:

2

Hi all..

In preprocessors, we can have switch between macros like,

#define BUFF(n) BUFF_##n

So, BUFF(1) would get replaced by BUFF_1, BUFF(2) would get replaced by BUFF_2 and song

Can this be applicable to C variables? i.e., choosing between similar variables dynamically. I understand it is a weird situation and can be handled using arrays or any other constructs.. but the situation demands me such situation.. could u plz help with this.. thanks in advance

+3  A: 

Sure, you can do this. preprocessor macros are just text replacements that are done to the code before compilation. You can't do this during runtime, though.

akr
+4  A: 

Yes, you can use that macro to apply BUFF_ to just anything. The preprocessor will expand macros and then the compiler will try to compile the result. The latter might fail, since if you use BUFF(+) you get BUFF_+ and that's not a valid variable name.

sharptooth
Quite handy for subroutines too. I can't recall details but I'm fairly sure in my past life we had a real life application of this construct, although to be fair some of our coding practices were a bit off the wall...
Tony van der Peet