Search code examples
cmacrosarmatmelstudio

pass macro definition to function taking reference as an argument


How can we pass a #defined macro to the function as an argument where the function accepts reference.

This is what I am trying the wrong way:

#define RST_REQ_REG 0x04
#define RESET_CMD   0x01

write_register(RST_REQ_REG, RESET_CMD, 1);


// Decleration of write_reg is
void write_register(uint8_t regAddr, uint8_t *writeByte, uint8_t writeByteLen);

Thanks !


Solution

  • You can't. write_register accepts a pointer as second argument, period.

    You probably want this:

    #define RST_REQ_REG 0x04
    #define RESET_CMD   0x01
    
    uint8_t cmdbuffer[1] = {command};
    
    write_register(RST_REQ_REG, cmdbuffer, 1);
    

    Maybe you want to wrap this into another function:

    void write_command(uint8_t regAddr, uint8_t command)
    {
       uint8_t cmdbuffer[1] = {command};
       write_register(regAddr, cmdbuffer, 1);
    }
    
    ...
    write_command(RST_REQ_REG, RESET_CMD);