I am trying to learn this CLI program but facing issue with an array of function pointer. I am trying to work with sample codes and playing with the same to get a better idea on concepts.
Its a syntax error and i am ubable to trace what is wrong here.
(*func_ptr[input.opInput])();
I am getting the error as
" error: called object is not a function or function pointer".
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
enum operation {ADD,
SEARCH,
LIST,
DELETE,
ERROR};
struct data{
int id;
char name[20];
char arch[20];
float rf_freq;
}; //This struct will be dynamically created
struct command {
enum operation opInput;
int id;
char name[20];
char arch[20];
float rf_freq;
};
//===========Function proto typing===============
void parseInput(char *inputStr,struct command *cmd);
void createNode(struct data *node);
void deleteNode(struct data *node);
//List of command input supported
void add();
void search();
void list();
void delete();
//===============================================
void (*func_ptr[4])={add,search,list,delete};
int main(void){
struct command input;
char cliInput[80];
printf("Welcome to CLI interface \n Please insert valid command\n");
printf("Valid input format is---->");
printf("wsn> command name arch rf_frequency\n");
printf("--------------------------------------------------------\n");
while(1){
printf("wsn>");
fgets(cliInput,80,stdin);
parseInput(cliInput,& input);
(*func_ptr[input.opInput])(); //ERROR
}//end of while 1
}//end of main
void parseInput(char *inputStr,struct command *cmd){
char *tknstrs[6];
int ii=0;
tknstrs[ii]=strtok(inputStr," \n");
while(tknstrs[ii]){
tknstrs[++ii]=strtok(NULL," \n");
}
if(strcmp(tknstrs[0],"add")==0)
cmd->opInput=ADD;
else if(strcmp(tknstrs[0],"search")==0)
cmd->opInput=SEARCH;
else if(strcmp(tknstrs[0],"list")==0)
cmd->opInput = LIST;
else if(strcmp(tknstrs[0],"delete")==0)
cmd->opInput = DELETE;
else
cmd->opInput = ERROR;
cmd->id=atoi(tknstrs[1]);
strcpy(cmd->name,tknstrs[2]);
strcpy(cmd->arch,tknstrs[3]);
cmd->rf_freq = atof(tknstrs[4]);
}//end of parseInput function
void add(){
}
void search(){
}
void list(){
}
void delete(){
}
Thank you for your effort, appreciate it.