1

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.

1 Answer 1

3

You are using a wrong declaration:

void (*func_ptr[4])={add,search,list,delete};

should be

void (*func_ptr[4])()={add,search,list,delete};
-------------------^^ You need also the type of the parameters or empty

Notice that you don't need to specify the number of elements of the array (you can avoid typing 4)

Also, prefer using void if the function doesn't take arguments:

void (*func_ptr[])(void)={add,search,list,delete};

and change the prototypes:

void add(void);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Keine Lust, it solved the issue. Simple syantx error on my end. Its giving segment fault error at ''strcpy(cmd->name,tknstrs[2]); strcpy(cmd->arch,tknstrs[3]);" any ideas on that?
Yes, you need space for store those strings, char *tknstrs[6]; is an array of pointers, you want a 2D array like char tknstrs[6][50];, another option is to reserve space for those pointers using tknstrs[0] = malloc(50); up to you if you want to use the heap or the stack.
Hey...the issue was with the size of tknstrs[]. Increasing the size of pointers to 10 solved the issue. Thank you Keine.
Note that if you give the size of an array as [] and initialize it, the compiler will make it the size of the initializer-list. Also, if you’re not going to be altering the contents of an array or variable, it’s wise to declare it const.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.