title: Function Pointer in Struct
body: <p>How do you use Function Pointer in Struct? 
  Specifically, given the following example, the program compiles but crash on run-time:</p>
  
  <p>In a header file</p>
  
  <pre><code> #ifndef __FUNCTION_IN_STRUCT_H_
   #define __FUNCTION_IN_STRUCT_H_
  
  
  struct functionDaemon {
       int id;
       //double (*funcp); // function pointer
       double  (*fp)(double);      // Function pointer
   };
  
   // #define NULL 'V'
  
   #endif /* _FUNCTION_IN_STRUCT_H_ */
  </code></pre>
  
  <p>In the C file:</p>
  
  <pre><code>#include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  
  #include "function_in_struct.h"
  
  extern struct functionDaemon *ftnAgent;
  
  void do_compute_sum (void) {
  
       void* agent;
      // struct functionDaemon *ftnAgent = (struct functionDaemon *) agent;
      struct functionDaemon *ftnAgent;
  
      double  sum;
  
      // Use 'sin()' as the pointed-to function
      ftnAgent-&gt;fp = sin;
      sum = compute_sum(ftnAgent-&gt;fp, 0.0, 1.0);
      printf("sum(sin): %f\n", sum);
  
  }
  </code></pre>
  
  <p>Please advise me.</p>