Clément
Clément

Reputation: 37

Is it possible to have #ifdef,#endif in functions's argument in c?

Hello I wanted to know if it is possible to have preprocessor language ine the argument of a function.

Something like this :

static void OnTxData( 
#ifdef MODULE1
    TxParams_t* params
#else
    Tx2Params_t* params
#endif
                       )
{
...
}

Upvotes: 0

Views: 507

Answers (2)

0___________
0___________

Reputation: 67820

Instead of hard to read:

static void OnTxData( 
#ifdef MODULE1
    TxParams_t* params
#else
    Tx2Params_t* params
#endif
                       )
{
...
}

#ifdef MODULE1
    #define TX_Params TxParams_t
#else
    #define TX_Params Tx2Params_t
#endif

static void OnTxData( TX_Params *prams)
{
...
}

or

#ifdef MODULE1
    typedef TxParams_t TX_Params
#else
    typedef Tx2Params_t TX_Params
#endif

static void OnTxData( TX_Params *prams)
{
...
}

or (IMO worse)

#ifdef MODULE1
static void OnTxData( TxParams_t *prams)
#else
static void OnTxData( Tx2Params_t *prams)
#endif
{
...
}

Upvotes: 1

Yes. You can use these literally anywhere (as long as they go on their own line). The preprocessor "works these out" before the rest of the compiler compiles anything.

So if MODULE1 is defined then this is the code that gets compiled:

static void OnTxData( 
    TxParams_t* params
                       )
{
...
}

and if it's not defined, then this is the code that gets compiled:

static void OnTxData( 
    Tx2Params_t* params
                       )
{
...
}

Upvotes: 1

Related Questions