c - Structure with array of function pointer -


#include <stdio.h>  void getsum(); void getdifference();  typedef void (*functionptr)();  // assign function's address functionptr arrayfp[2] = {getsum, getdifference};  struct true {     int a;     int b;     functionptr arrayfp[2]; //syntax may wrong  } w = { 5, 6, arrayfp[0] };  int main() {     w.arrayfp[0];    //syntax wrong      return 0; }  void getsum() {     printf("i greatest"); }  void getdifference() {     printf("i not greatest"); } 

when initializing structure, initialize array first function pointer, not actual array. in fact, structure contains array need initialize actual members of array, either or change pointer.

then when want call it, use function pointer normal function.


so structure, either

struct {     int a;     int b;     functionptr arrayfp[2]; } w = {     5, 6, { getsum, getdifference } }; 

or do

struct {     int a;     int b;     functionptr *arrayfp; } w = {     5, 6, arrayfp }; 

note: don't use symbol true name, might defined if include <stdbool.h>.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -