title: C - Struct across multiple files
body: <p>I'm having a problem reading and writing to structs across multiple files. Essentially I need to write to variables within a struct that are later checked during a timer interrupt.  When this happens, the new timer value is taken as a member in that struct. At the moment I hard set these timer values in a while(1) loop, but later these values will be taken from some algorithm. I'm not quite sure if I'm doing this reading of struct members correctly. The project compiles, however during runtime, it sets the timers to random values. GDB degugging confirmed they are correct however.</p>
  
  <p>If I set the timer values directly, everything works file.</p>
  
  <p>This is an embedded project on an ARM cortex M4.</p>
  
  <p>I have a structure defined in types.h</p>
  
  <pre><code>#ifndef __TYPES_H
  #define __TYPES_H
  
  typedef struct { uint32_t a; uint32_t b; uint32_t c;} myStruct;
  
  #endif
  </code></pre>
  
  <p>Then in main.c</p>
  
  <pre><code>#include &lt;types.h&gt;
  
  myStruct hello
  
  int main(void){
      while(1){
          hello.a = 10;
          hello.b = 43;
          hello.c = 98;
      }
  }
  </code></pre>
  
  <p>Then in interrupt.c</p>
  
  <pre><code>#include &lt;types.h&gt;
  
  myStruct hello
  
  int count = 0;
  
  void timer_IRQHandler(void){
      if(interrupt != RESET){
          switch(count){
             case 0:
                 timerSet(hello.a); // if i just put a number here, it works fine
                 count++;
                 break;
             case 1:
                 timerSet(hello.b);
                 count++;
                 break;
             case 2:
                 timerSet(hello.c);
                 count++;
                 break;
          }
     resetInterrupt();
     }
  }
  </code></pre>
  
  <p>--- SOLUTION ---</p>
  
  <p>Ok I've figured it out, could do with moving things around but otherwise it works like this:</p>
  
  <p>types.h</p>
  
  <pre><code>#ifndef __TYPES_H
  #define __TYPES_H
  
  typedef struct { uint32_t a; uint32_t b; uint32_t c;} myStruct;
  
  #endif
  </code></pre>
  
  <p>Then in main.c</p>
  
  <pre><code>#include &lt;types.h&gt;
  
  myStruct volatile hello = {10,10,10};
  
  int main(void){
      while(1){
          hello.a = 10;
          hello.b = 43;
          hello.c = 98;
      }
  }
  </code></pre>
  
  <p>Then in interrupt.c</p>
  
  <pre><code>#include &lt;types.h&gt;
  
  extern myStruct hello
  
  int count = 0;
  
  void timer_IRQHandler(void){
      if(interrupt != RESET){
          switch(count){
             case 0:
                 timerSet(hello.a); // if i just put a number here, it works fine
                 count++;
                 break;
             case 1:
                 timerSet(hello.b);
                 count++;
                 break;
             case 2:
                 timerSet(hello.c);
                 count++;
                 break;
          }
     resetInterrupt();
     }
  }
  </code></pre>
  
  <p>The extern seems to have solved the problem of getting the struct across different files, and the initial value declaration of {10,10,10} has, I think solved some memory allocation problem. The code compiles, but doesn't hold correct values without it. I don't know what volatile does yet, no difference if I remove it. Something I shall read up on.</p>