title: How can I encapsulate a function like this in C?
body: <p>I want write a struct to encapsulate some function like this:</p>
  <pre><code>
  typedef struct _Operate {
      void *              op_fun;
      size_t         result_type;
      unsigned int     arg_count;
      size_t          arg_type[];
  } * Operate;
  
  </code></pre>
  <p>Here <code>op_fun</code> is the function pointer this operation does. <code>result_type</code> is the type that function returns. <code>arg_count</code> is count of arguments, and <code>arg_type</code> keep the type of arguments. In other words, the <code>op_fun</code> is like this:</p>
  <pre><code>result_type ( * op_fun)(type_1 arg_1, type_2 arg_2, ... , type_N arg_N);
  </code></pre>
  <p>The <code>N</code>, at first may be not known until <code>arg_cuont</code> has been given. <code>type_1</code> is <code>arg_type[1]</code>, and <code>type_2</code> is <code>arg_type[2]</code> and so on.</p>
  <p>And my question is: <strong>How to rebuild the function pointer?</strong> Because when the function pointer became to <code>op_fun</code>, it became a <code>void *</code>. The information of its arguments and return type has been lost. I have to rebuild it by only given <code>result_type</code>, <code>arg_count</code> and <code>arg_type</code>.</p>
  <p>At this condition it saves my lot time to develop, like this:</p>
  <pre><code>void traverseTree(TreeNode tree_root, Operate operate)
  { 
      for(int i = 0; i &lt; tree_root-&gt;n_children; i++) {
          traverseTree(tree_root-&gt;children[i], operate);
      } 
      doOperate(operate, tree_root); 
  }
  </code></pre>
  <p>So that I don't have to write these code every times when traverse the tree( it acturally saved my time).</p>
  <p>But I want it is not a compile-time behavior, that is just a
  syntactic sugar. I wnat know if there is a methord to realize it.</p>