I write a command interpreter that parses a command and its arguments and I would like to find a way to pass them to various non-variadic functions like this:
typedef boolean (*CommandExecuteCallback)(char* arg1, ...); // n argument variadic function
char command[CMD_WORD_MAXLEN+1]; // currently parsed command
char args[CMD_MAX_ARGUMENTS][CMD_WORD_MAXLEN+1]; // currently parsed arguments
const char *commands[CMD_MAX_COMMANDS]; // available commands
CommandExecuteCallback commandf[CMD_MAX_COMMANDS]; // available execution functions
executeCommand(char *buf)
{
// find command and parse args
// call it
commandf[i](this->args);
}
bool cmd_blink(char* onOff) { ... }
bool cmd_something(char* arg1, char* arg2) { ... }
I am not sure this is possible in C/C++ and definitely not documented here: https://en.cppreference.com/w/cpp/utility/variadic
UPDATE:
Variadic functions cannot be dynamically called in C. The solution is to pass the args as array ´char args[][]´ or as std::vector (std:: is too big for Microcontrollers so the 1st solution prevails) which makes the function signatures match. Thanks JoJo and fabian for the leading hints. I will post the solution below.