How to call pointer to function defined in typedef struct

不打扰是莪最后的温柔 提交于 2019-12-06 15:47:19

You have declared your counter_func member to be a pointer to an int, not a function pointer , while you have something resembling a function pointer declaration in your option values. Here's what you want (assuming your return type is int )

typedef struct OptionValueStruct{
  char counter_name[OPTION_LINE_SIZE];
  int (*counter_func)(char*, char**);
} OptionValueStruct_t;

const OptionValueStruct_t option_values[] = {
  {"Counter1", parseCounter1},
  {"Counter2", parseCounter2},
};

for(int i = 0; i< sizeof(option_values)/sizeof(OptionValueStruct_t); i++){
  result = option_values[i]->counter_func(opt_name, opt_val); 
  // don't know what you relly want to do with result further on..
}

If you are compiling as C code (as your tag suggests), then you should change the type of option_values[] and option to OptionValueStruct_t. In C++, however, this is OK.

Alternatively, you can eliminate the trailing _t from the custom type name.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!