title: Dynamically creating functions in C
body: <p>How can I dynamically create a function in C?</p>
  
  <p>I try to summarize my C problem as follows:</p>
  
  <ul>
  <li><p>I have a matrix and I want to be able to use some function to generate its elements.</p></li>
  <li><p>function has no arguments</p></li>
  </ul>
  
  <p>Hence I define the following: </p>
  
  <pre><code>typedef double(function)(unsigned int,unsigned int);
  
  /* writes f(x,y) to each element x,y of the matrix*/
  void apply(double ** matrix, function * f);
  </code></pre>
  
  <p>Now I need to generate constant functions within the code. I thought about creating a nested function and returning its pointer, but GCC manual (which allows nested functions) says:</p>
  
  <blockquote>
    <p>"If you try to call the nested function through its address after the
    containing function has exited, all hell will break loose."</p>
  </blockquote>
  
  <p>which I would kind of expect from this code...</p>
  
  <pre><code>function * createConstantFunction(const double value){
   double function(unsigned int,unsigned int){
     return value;
   }
   return &amp;function;
  }
  </code></pre>
  
  <p>So how can I get it to work?</p>
  
  <p>Thanks!</p>