title: Initializing an array of structs (compiler error: initializer element is not constant)
body: <p>I am trying to initialize a C struct using the following code:</p>
  
  <pre><code>/* header file */
  
  typedef struct _funky {
     int  func_id;  /* I know this should be intptr_t, but ignore for now ... */
     char func_name[MAX_FUNC_NAME_LEN];
  } Funky;
  
  
  double func1(double d1, double d2, double d3);
  double func2(double d1, double d2, double d3);
  double func3(double d1, double d2, double d3);
  double func4(double d1, double d2, double d3);
  
  
  /* .c file */
  Funky fk[4] = {
                   {(int)func1, "func1"}, /* &lt;- gcc barfs here ... */
                   {(int)func2, "func2"},
                   {(int)func3, "func3"},
                   {(int)func4, "func4"}
                };
  </code></pre>
  
  <p>When I attempt to compile this (gcc 4.6.3), I get the following errors:</p>
  
  <pre><code>error: initializer element is not constant
  error: (near initializer for 'fk[0].func_id')
  </code></pre>
  
  <p>How I can fix this error?</p>
  
  <p><strong>[[Edit]]</strong></p>
  
  <p>After a brief chat with ouah, I have found the reason for this error - it is to do with the function definitions being used to initialize the array. Some of the definition are in different translation units, and others in different modules. All of which means (IIUC) that the functions will not be defined at compile time. </p>
  
  <p>Short of writing an initialization function (which will require extensive mod in existing code) I'm not sure how to solve this - and I'm not sure how it compiled under previous versions of gcc either.</p>