I have the following type definition:
typedef struct {
int (*function)(int argc, char *argv[]);
char *name;
} command_t;
The member function
is a function pointer and the member name
is a string which will store the name of the function.
To initialize a variable of type command_t
, I wrote the following macro:
#define COMMAND(x) (command_t){.function = x, .name = #x}
Here's how I currently initialize an array of command_t
:
int ls(int argc, char *argv[]);
int echo(int argc, char *argv[]);
int cat(int argc, char *argv[]);
int mkdir(int argc, char *argv[]);
command_t cmd_list[] = {COMMAND(ls), COMMAND(echo), COMMAND(cat), COMMAND(mkdir)};
I would like to be able to initialize an array of command_t
as such:
command_t cmd_list[] = COMMAND(ls, echo, cat, mkdir);
or
command_t cmd_list[] = {COMMAND(ls, echo, cat, mkdir)};
I know that COMMAND
has to be a variadic macro to do so but I don't know how to write it.