title: Calling a function from function pointer in struct
body: <p>I want to write a wrapper for the callback function for progress information of libcurl in C.
  This wrapper should calculate some data which are supposed to be encapsulated in a library.</p>
  
  <p>Here's a description of the basic idea: (I hope this clarifies things)</p>
  
  <p>The user should implement a callback function xFunc and initialize a data structure to store data for the callback function (called xData). Then he should call a wrapper function aFunc. As argument he provides some connection related data and a pointer to xFunc and xData. In aFunc some things are calculated which are required to call a second function bFunc. The xFunc and xData pointer are also passed to this function. In this function, a second data structure YData is initialized. This structure holds a pointer to the user provided xFunc and xData and some other data which are required for calculations. Now the libcurl related things get initialized. As a callback for the upload progress, a second callback function yFunc is implemented in the library. In this function the user provided xFunc should be called with xData and other calculated values.</p>
  
  <p>Now here's the code of my current implementation (I stripped all the calculation code and renamed the functions and structures to math the description above so it looks a bit strange. I hope it isn't too abstract now):</p>
  
  <p><em>main.c</em></p>
  
  <pre><code>int xFunc(void *xData, long int param1, long int param2) {
      //do something with the data
      return 0;
  }
  
  int main(void){
  
      xpData *xData;
      xData-&gt;value = 0;
  
      aFunc(xFunc, p);
  
      return 0;
  }
  </code></pre>
  
  <p><em>curlutils.h</em></p>
  
  <pre><code>typedef struct {
      int value;
  } xpData;
  
  int xFunc(void *xData, long int param1, long int param2);
  int aFunc(int (*xFunc)(void*, long int, long int), void xData);
  int bFunc(int (*xFunc)(void*, long int, long int), void xData);
  </code></pre>
  
  <p><em>curlutils.c</em></p>
  
  <pre><code>typedef struct {
      int (*xFunc)(void*, long int, long int);
      void *data;    
  } ypData;
  
  
  int yFunc(void *p, double dltotal, double dlnow, double ultotal, double ulnow){
      ypData *pd;
      pd = (ypData*) p;    
      return pd-&gt;xFunc(pd-&gt;data);
  }
  
  int bFunc(int (*xFunc)(void*, long int, long int), void xData){
      CURL *curl;
      curl_global_init(CURL_GLOBAL_ALL);
      curl = curl_easy_init();
      if (curl){ //stripped many curl initialization stuff
          ypData *yData;
          yData-&gt;data = xData;
          yData-&gt;xFunc = xFunc;
          curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, yFunc);
          curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &amp;yData);
          curl_easy_perform(curl);
      }
      return 0;
  }
  
  int aFunc(int (*xFunc)(void*, long int, long int), void *xData){
      //calculate something useful
      bFunc(xFunc, xData);
      return 0;
  }
  </code></pre>
  
  <p>The problem is in yFunc where I want to call xFunc. The program crashes with the error:</p>
  
  <pre><code>Illegal instruction
  </code></pre>
  
  <p>Why is this code not working?</p>