title: Set struct field with function pointer in C
body: <p>I'm messing around with C and i would like to simulate a constructor for a class but using C.
  I've a structure with two fields, one <code>int</code> and a pointer to a function, like this</p>
  <pre><code>typedef struct elem {
      int a;
      void (*initVal)(struct elem*);
  } element;
  </code></pre>
  <p>The function <code>initVal</code> is the following</p>
  <pre><code>void initVal(struct elem* el) {
      el-&gt;a = 5;
  }
  </code></pre>
  <p>The main idea behind this function is to set the field <code>a</code> of the struct itself to 5.
  In the main:</p>
  <pre><code>int main() {
      element a;
      (a.initVal)(&amp;a);
      printf(&quot;%d\n&quot;, a.a);
  
      return 0;
  }
  </code></pre>
  <p>My goal i to have printed <code>5</code> in the main, but this program throws a runtime error. What is wrong here? Is it possible to call a function pointer to set a field of the structure where it is defined in? Hope this is clear.</p>