tags:

views:

58

answers:

3
#define C_TX_ TX_
#define C_RX_ RX_

enum Test
{
        C_TX_MAC = 0x0100, // Pre-Processor should replace C_TX_ to TX_
        C_RX_MAC = 0x0101  // But Not Working.
};

int main(int argc, char *argv[])
{
    cout << TX_MAC; // HOW TO PRINT ?
    cout << RX_MAC; // HOW TO PRINT ?

    return true;

}

Please Help. Thanks in Advance

+2  A: 

The pre-processor only operates on strings that are entire tokens. There would be chaos otherwise.

Try:

#define C_TX_MAC TX_MAC
#define C_RX_MAC RX_MAC
jeffamaphone
I have big variable name like C_ADDR_TX_MAC_DESTINATION_ADDRESS_FIELD_WORD_0_0 where i need to replace only C_ADDR_TX_ with TX_ only.
mahesh
@mahesh: But what is the "need"? I'm sure there are other solutions.
GMan
Well, tough. The pre-processor can't operate on substrings or things like `#define tf 4` would break every call to `printf()`. You have to use the entire token.
jeffamaphone
+2  A: 

You cannot split a token with the pre-processor. You need to

#define C_RX_MAC RX_MAC
#define C_TX_MAC TX_MAC

(Of course there's ugly solutions such as adding a pre-pre-processing step:

sed s/C_ADDR_// x.cpp | g++ -x c++ -

But sed doesn't know about the context. It will replace strings e.g. cout << "And C_ADDR_RX = " with cout << "And RX = ".)

KennyTM
A: 

As stated in the other answers the pre-processor uses the whitespace to work out where the token is defined, and cannot replace it 'part way through". Perhaps you could try a "Find/Replace In Files" to rename the variables in your source code directly. Visual Studio's Find and Replace function can be used to replace any occurences in any folders/subfolders, or if you don't run with the Microsoft there's some other programs like NotePad++ that offer the same functionality. Both also support Regular Expressions for better targeted find/replace queries

tjohns20