title: Is it legal to index into a struct?
body: <p>Regardless of how 'bad' the code is, and assuming that alignment etc are not an issue on the compiler/platform, is this undefined or broken behavior?</p>
  
  <p>If I have a struct like this :-</p>
  
  <pre><code>struct data
  {
      int a, b, c;
  };
  
  struct data thing;
  </code></pre>
  
  <p>Is it <strong>legal</strong> to access <code>a</code>, <code>b</code> and <code>c</code> as <code>(&amp;thing.a)[0]</code>, <code>(&amp;thing.a)[1]</code>, and <code>(&amp;thing.a)[2]</code>?</p>
  
  <p>In every case, on every compiler and platform I tried it on, with every setting I tried it 'worked'. I'm just worried that the compiler might not realize that <strong>b</strong> and <strong>thing[1]</strong> are the same thing and stores to 'b' might be put in a register and thing[1] reads the wrong value from memory (for example). In every case I tried it did the right thing though. (I realize of course that doesn't prove much)</p>
  
  <p>This is not my code; it's code I have to work with, I'm interested in whether this is <strong>bad</strong> code or <strong>broken</strong> code as the different affects my priorities for changing it a great deal :)</p>
  
  <p>Tagged C and C++ . I'm mostly interested in C++ but also C if it is different, just for interest.</p>