Array of macros in c — is it possible

前端 未结 2 869
一向
一向 2021-01-22 02:38

I was wondering if it is possible to create something like an array of macros. I\'ve implemented the following code which works:

struct led_cmds_
{ 
    ioport_pi         


        
2条回答
  •  时光取名叫无心
    2021-01-22 03:25

    An array as tofro's answer is the way to go. However in cases that couldn't be solved simply with an array then there's another way with switch

    #define SPECIFICPIN(X) (LED##X##_PIN)
    
    void setpin(int pin, int value)
    {
        switch (pin)
        {
        case 1:
            SPECIFICPIN(1) = value;
            doSomething(); // if needed
            break;
        case x: ...
        default: ...
        }
    }
    

提交回复
热议问题