Embedded C - Too many arguments to function (pointer)

前端 未结 4 644
刺人心
刺人心 2020-12-19 11:57

I am trying to invoke the following macro in my .cpp file:

#define IAP_ROM_LOCATION                0x1FFF1FF1UL
#define IAP_EXECUTE_CMD(a, b)           ((voi         


        
相关标签:
4条回答
  • 2020-12-19 12:30

    You can also do it with macros only:

    #define IAP_ROM_LOCATION        0x1FFF1FF1UL
    #define IAP_FUNC_SIGNATURE      void (*)(int, int)
    #define IAP_EXECUTE_CMD(a, b)   ((IAP_FUNC_SIGNATURE)(IAP_ROM_LOCATION))((a), (b))
    
    0 讨论(0)
  • 2020-12-19 12:32
    (void (*)())(IAP_ROM_LOCATION)
    

    This part casts IAP_ROM_LOCATION to a pointer to a function that takes no arguments and returns nothing (void (*)()). Hence you get an error when you want to pass any arguments to that function.

    0 讨论(0)
  • 2020-12-19 12:32

    OK, my C is rather rusty, but it seems to me that your IAP_EXECUTE_CMD() macro is casting the unsigned long address as a pointer to a function which returns void and accepts zero arguments. Therefore, any arguments passed to the function call would be too many.

    0 讨论(0)
  • 2020-12-19 12:45

    For readability, define a signature for the function to be called:

    typedef void signature_t(int, int);
    

    Then you can cast your ROM location

    #define IAP_EXECUTE_CMD(a, b)  ((signature_t*)IAP_ROM_LOCATION) ((a),(b))
    

    and with a recent GCC (current version of GCC is 4.6) I would make that an inline function

    static inline void iap_execute_cmd(int a, int b) {
        ((signature_t*)IAP_ROM_LOCATION) ((a),(b));
    }
    
    0 讨论(0)
提交回复
热议问题