title: Initializing a Global Struct in C
body: <p>What is the best way to accomplish the following in C?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  struct A
  {
      int x;
  };
  
  struct A createA(int x)
  {
      struct A a;
      a.x = x;
      return a;
  }
  
  struct A a = createA(42);
  
  int main(int argc, char** argv)
  {
      printf("%d\n", a.x);
      return 0;
  }
  </code></pre>
  
  <p>When I try to compile the above code, the compiler reports the following error:</p>
  
  <p>"initializer element is not constant"</p>
  
  <p>The bad line is this one:</p>
  
  <pre><code>struct A a = createA(42);
  </code></pre>
  
  <p>Can someone explain what is wrong?  I'm not very experienced in C.  Thanks!</p>