I'm trying to build a serial command interpreter, so I want to store my commands in an array. I want each command to have a name and a function pointer so that I can compare the command name to what I typed into and then call the function. I'm not that good with C, so please help! Here is what I have so far.
The command array will be an array of structs. Each struct will have a string and a function pointer. There are errors here, but I don't know how to fix them. These are done before main.
typedef struct cmdStruct {
char cmd[16];
void (*cmdFuncPtr)(void);
}CmdStruct;
void (*ledFuncPtr)(void);
void (*cmd2FuncPtr)(void);
// assign pointers to functions
ledFuncPtr = &LedFunction;
cmd2FuncPtr = &Cmd2Function;
//build array of structs
CmdStruct cmdStructArray[] = cmdStructArray = { {"led", ledFuncPtr },
{"cmd2", cmd2FuncPtr }, };
Later on, I will go through the struct array to compare it to the received command.
// go through the struct array to do string comparison on each struct's string member
for (int i = 0; i < sizeof(cmdStructArray); i++) {
// string comparison of received command and string of struct
if(strcmp(cmdStructArray[i].cmd, receivedCmd)==0) {
// dereference function pointer
(*cmdStructArray[i].cmdFuncPtr)(void);
}
}
What parts am I doing wrong, and how do I fix them?