title: How to dereference and call a function of a struct in C
body: <p>Lets say we have this struct:</p>
  
  <pre><code>typedef struct foo {
     int (*func)(char *param);
  } foo;
  </code></pre>
  
  <p>and we have this prototype:</p>
  
  <pre><code>void bar(foo *f){
    char *t = something
    int a = f-&gt;func(t)
    do stuff with a
  }
  </code></pre>
  
  <p>but this gives a segfault when initializing int a. I also tried <code>int a = f-&gt;func(t)</code> but this also leads to a segfault</p>
  
  <p>I call bar by </p>
  
  <pre><code>foo *baz = malloc(sizeof(foo));
  baz-&gt;func = &amp;somefunction;
  bar((foo *) &amp;baz);
  </code></pre>