Search code examples
cc-preprocessordirectivepreprocessor-directive

Sending parameter to a #define


I wonder to know is it possible to send a parameter to a #define macro for selecting different output

For example:

#define Row(1) LPC_GPIO0
#define Row(2) LPC_GPIO3
#define Row(3) LPC_GPIO2

Then in my code I create a loop for sending the parameter

Row(x)

Solution

  • This macro syntax doesn't exist.

    Moreover, it can't possibly exist, because macros are expanded before the compiler compiles the code. If your x isn't a compile time constant, there would never be a way to determine what to replace in the source code for the macro invocation.

    If you need to index some values, just use an array, e.g. (assuming these constants are integers):

    static int rows[] = { 0, LPC_GPIO0, LPC_GPIO3, LPC_GPIO2 };
    

    Writing

    rows[x]
    

    would have the effect you seem to have expected from your invalid macro syntax.