title: Call function by function pointer from array of structs
body: <p>I'm new to C and I'm trying to create an array of structs which contain a function pointer. </p>
  
  <p>Calling the function (and accessing the members) from the struct directly works as expected, but accessing it from the array by looping through it doesn't (accessing the members works).</p>
  
  <p>I've found a similar question already, but I would like to know why my program which I put together from various tutorials and posts crashes when I try to call my function by accessing the function from the array. </p>
  
  <p>structs.h</p>
  
  <pre><code>typedef struct {
    char *functionId;
    void (*callback)(unsigned char *data);
    char *name;
    char *description;
  } Function;
  
  typedef struct {
    Function *fArr;
    size_t used;
    size_t size;
  } Array;
  </code></pre>
  
  <p>main.c</p>
  
  <pre><code>   #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "structs.h" 
  
     Array functionArray;
  
  void function1(int *i){
      printf("PRINT: %d\n", i);
  }
  
  void function2(char *c[]){
      printf("PRINT: %s\n", c);
  }
  
  void addFunction(Function f){
      insertFunctionArray(&amp;functionArray, f);
  }
  
  int main()
  {
      initFunctionArray(&amp;functionArray, 5);
  
      //create new struct for function and assign function to callback member
      Function fun;
      fun.name = "Function1";
      fun.functionId = "FUNCTION1";
      fun.description = "Description for Function 1";
      fun.callback = &amp;function1; // &lt;-- doesn't seem to matter if I assign it as a value or as a pointer?!
  
      Function fun2;
      fun2.name = "Function2";
      fun2.functionId = "FUNCTION2";
      fun2.description = "Description for Function 2";
      fun2.callback = &amp;function2;
  
      fun2.callback("{fun:\"test\"}");
  
      fun.callback(17);
  
      //store functions in array
      addFunction(fun);
      addFunction(fun2);
  
      unsigned int i = 0;
      for(i; i&lt;functionArray.used; i++)
        {
          printf("PRINT: %s\n", functionArray.fArr[i].functionId); // &lt;-- this works
          functionArray.fArr[i].callback("777"); // &lt;-- this doesn't work
          }
        }
  
      return 0;
  }
  
  void initFunctionArray(Array *fa, size_t initialSize) {
    fa-&gt;fArr = (Function *)malloc(initialSize * sizeof(Function));
    fa-&gt;used = 0;
    fa-&gt;size = initialSize;
    unsigned int i = 0;
    for(i; i&lt;initialSize; i++)
        {
            memset(&amp;fa-&gt;fArr[i],0,sizeof(Function));
        }
  }
  
  void insertFunctionArray(Array *fa, Function f) {
    if (fa-&gt;used == fa-&gt;size) {
      fa-&gt;size *= 2;
      fa-&gt;fArr = (Function *)realloc(fa-&gt;fArr, fa-&gt;size * sizeof(Function));
    }
    // Copy name
    fa-&gt;fArr[fa-&gt;used].name = (char*)malloc(strlen(f.name) + 1);
        strcpy(fa-&gt;fArr[fa-&gt;used].name, f.name);
    // Copy ID
    fa-&gt;fArr[fa-&gt;used].functionId=f.functionId;
    fa-&gt;used++;
  }
  </code></pre>
  
  <p>Thanks &amp; BR</p>