fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5. char c; // several simple type variables...
  6. int(*eventhandler)(int param); // function pointer member (maybe, it would do with double indirection...?)
  7. } BtnStruct;
  8.  
  9. int function0(int param) // say, there is a similar function1()
  10. {
  11. int retval=0;
  12. printf("%s\n", __FUNCTION__);
  13.  
  14. // do something
  15.  
  16. return(retval);
  17. }
  18. int function1(int param) // say, there is a similar function1()
  19. {
  20. int retval=0;
  21. printf("%s\n", __FUNCTION__);
  22. // do something
  23.  
  24. return(retval);
  25. }
  26.  
  27. int main(){
  28.  
  29. BtnStruct Btn0; // BtnStruct variable
  30. BtnStruct Btn1;
  31.  
  32. BtnStruct *BtnStructArray[2]; // array of pointers, pointed to BtnStruct type variables
  33.  
  34. BtnStructArray[0] = &Btn0; // fill the array with addresses of BtnStruct variables
  35. BtnStructArray[1] = &Btn1;
  36.  
  37. int returnvalue; // just for test
  38.  
  39.  
  40. // In Run time:
  41.  
  42. BtnStructArray[0]->eventhandler = function0; // I try to give the address of the function, to the function pointer member
  43. BtnStructArray[1]->eventhandler = function1;
  44.  
  45.  
  46. returnvalue = BtnStructArray[0]->eventhandler(10); // here I want to call the pointed function with parameter
  47. (void)returnvalue; // But the function is not invoked
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
function0