Let’s say i am communicating with a serial-port device and have a significant number of commands (74) for controlling such a device. Wich is the best way to store and use them?
Of course i can organize them the following way:
static char *cmd_msgs[] =
{
"start",
"stop",
"reset",
"quit",
"",
"",
"",
"",
...
};
Or human readable:
char cmd_start_str[] = "start";
...
char cmd_quit_str[] = "quit";
Can someone point to a working example dealing such a task?
The first approach is fine – don’t use a lot of global variables with a unique name, they’re hard to reference especially when you want to loop through them. That’s what the array of strings is for (your first way). If you want human readable code (which you should want), use a sensibly named enumeration of which the values correspond to the actual command strings. So do something like
This way you can easily reference your commands by indexing the array, but you still have himan readability by writing
cmds[COMMAND_SAYHELLO]etc.