title: Using structs in multiple files
body: <p>I am trying to call a function in main.c from io.h that reads data from a file, stores that data into multiple structs, then somehow lets me pass the different structs as arguments in later functions in main. Those later functions will be defined in other files, such as alg.h.</p>
  
  <p>How do I go about doing this? Do I use extern and make the structs global and put them in a separate file? Is it possible to have a function from alg.h have a return type of one of the structs? Does it depend on the order of my includes?</p>
  
  <p>The code pasted below complies and works, but any attempt to move either of the structs causes the program to not compile. </p>
  
  <p>Also, is it possible to have, for example, a struct declared in alg.h, then functions that have that struct as a parameter declared later in alg.h. Then in main.c, you initialize and pass the struct into a function declared in io.h, give the struct some values, have it returned to main.c, then pass that into the function declared in alg.h? I know that sounds like a class, but I need a C solution and I only need one instance of the struct floating around.</p>
  
  <p>Thanks.</p>
  
  <p>io.h</p>
  
  <pre><code>struct s1 {
      int num1;
      double num2;
  };
  
  struct s2 {
      int num3;
      double num4;
  };
  
  void io_init(struct s1*, struct s2*);
  </code></pre>
  
  <p>io.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "io.h"
  
  void io_init(struct s1* s1i, struct s2* s2i)
  {
      s1i-&gt;num1 = 5;
      s1i-&gt;num2 = 2.4;
  
      FILE *fp;
      char line[80];
  
      fp = fopen("input.txt","rt");
  
      fgets(line, 80, fp);
      sscanf(line,"%i",&amp;s2i-&gt;num3);
      fgets(line, 80, fp);
      sscanf(line,"%i",&amp;s2i-&gt;num4);
  
      fclose(fp);
  }
  </code></pre>
  
  <p>alg.h</p>
  
  <pre><code>void ga_init(struct s1);
  </code></pre>
  
  <p>alg.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #include "io.h"
  #include "ga.h"
  
  void ga_init(struct s1 s1i)
  {
      printf("%i", s1i.val1);
  }
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #include "io.h"
  #include "ga.h"
  
  int main() {
      struct s1 s1i;
      struct s2 s2i;
  
      io_init(&amp;s1i, &amp;s2i);
      ga_init(s1i);
  
      return 0;
  }
  </code></pre>