Search code examples
cstm32

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
                       )
{
...
}

Solution

  • 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
    {
    ...
    }