title: Storing a global struct variable inside another global struct in C
body: <p>I’m trying to figure out a way to use nested global structs as a sort of API namespacing for my C library.</p>
  
  <p>Specifically, I want to expose a single <code>Primary</code> ‘namespacing struct,’ that contains other such structs (such as <code>Primary.Secondary</code>), that themselves contain function pointers (<code>Primary.Secondary.a_function()</code>).</p>
  
  <p>I’ve abstracted out the following (relatively) simple example of what I want to do:</p>
  
  <p><code>main.c</code>:</p>
  
  <pre><code>#include "Primary.h"
  
  int main () {
    Primary.Secondary.a_function();
    return 0;
  }
  </code></pre>
  
  <p><code>Primary.h</code>:</p>
  
  <pre><code>#if !defined(SECONDARY_H)
  # include "Secondary.h"
  #endif
  
  struct Primary_struct {
    struct Primary__Secondary_struct  Secondary;
  } extern Primary;
  </code></pre>
  
  <p><code>Primary.c</code>:</p>
  
  <pre><code>#include "Primary.h"
  
  struct Primary_struct Primary = {
    .Secondary = Primary__Secondary
  };
  </code></pre>
  
  <p><code>Secondary.h</code>:</p>
  
  <pre><code>struct Primary__Secondary_struct {
    void  (*a_function) (void);
    void  (*another_function) (void);
  } extern Primary__Secondary;
  </code></pre>
  
  <p><code>Secondary.c</code>:</p>
  
  <pre><code>#include "Secondary.h"
  
  #include &lt;stdio.h&gt;
  
  void  Primary__Secondary__a_function  (void);
  void  Primary__Secondary__another_function  (void);
  
  struct Primary__Secondary_struct {
    .a_function       = Primary__Secondary__a_function,
    .another_function = Primary__Secondary__another_function
  } extern Primary__Secondary;
  
  void Primary__Secondary__a_function(void) {
    Primary.Secondary.another_function();
  }
  
  void Primary__Secondary__another_function(void) {
    printf("run!\n");
  }
  </code></pre>
  
  <p>When I attempt to compile this, I run into the following compiler error:</p>
  
  <pre><code> &gt; C -O0 Primary.c Secondary.c main.c
  Primary.c:3:33: error: initializer element is not a compile-time constant
  struct Primary_struct Primary = {
                                  ^
  1 diagnostic generated.
  </code></pre>
  
  <p>I should note, ideally, both the <code>Primary</code> and <code>Primary__Secondary</code> variables would be <code>const</code>. I was worried that the added complexity would exacerbate the problem… so for now, I’ve left that aspect out.</p>
  
  <p>The problem seems to be that, for some reason, even when set as <code>const</code>, and containing only elements present at compile time, the <code>Primary__Secondary</code> struct is not a compile-time constant, and thus cannot be stored in another struct at compile-time. I can probably work around this by setting up all of the interfaces at runtime, but… that seems like a really hacky solution. I’m looking for any alternative solutions to this problem, that the more C-fu of you than I can come up with.</p>
  
  <p><em>(<strong>Note</strong>: This is related to <a href="https://stackoverflow.com/questions/2205579/how-can-i-define-two-global-const-variables-to-the-same-value-in-a-c-module">this question</a>, but is substantially different, and quite a bit more specific.)</em></p>