title: union of structs sharing same first members
body: <p>I have been looking into an un-traditional way of achieving struct "polymorphism" in pre-C11 C. Let's say we have 2 structs:</p>
  
  <pre><code>struct s1 {
      int var1;
      char var2;
      long var3;
  };
  
  struct s2 {
      int var1;
      char var2;
      long var3;
      char var4;
      int var5;
  };
  </code></pre>
  
  <p>On most compilers, we could safely cast between pointers to the two and then access the common first members if no padding takes place. However, this is not standartized behaviour. </p>
  
  <p>Now, I found the following line in the C standard as far as C89:</p>
  
  <blockquote>
    <p>One special guarantee is made in order to simplify the use of unions: If a union contains several structures that share a common initial sequence, and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them. Two structures share a common initial sequence if corresponding members have compatible types for a sequence of one or more initial members.</p>
  </blockquote>
  
  <p>It also states the following:</p>
  
  <blockquote>
    <p>A pointer to a union object, suitably cast, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.</p>
  </blockquote>
  
  <p>Now, if I create a union of these two structs:</p>
  
  <pre><code>union s2_polymorphic {
      struct s1 base;
      struct s2 derived;
  };
  </code></pre>
  
  <p>And use it this way:</p>
  
  <pre><code>union s2_polymorphic test_s2_polymorphic, *ptest_s2_polymorphic;
  struct s2 *ptest_s2;
  struct s1 *ptest_s1;
  
  ptest_s2_polymorphic = &amp;test_s2_polymorphic;
  
  ptest_s2 = (struct s2*)ptest_s2_polymorphic;
  
  ptest_s2-&gt;var1 = 1;
  ptest_s2-&gt;var2 = '2';
  
  ptest_s1 = (struct s1*)ptest_s2;
  
  printf("ptest_s1-&gt;var1 = %d\n", ptest_s1-&gt;var1);
  printf("ptest_s1-&gt;var2 = %c\n", ptest_s1-&gt;var2);
  </code></pre>
  
  <p>Which compiles and runs fine and gives, on <strong>gcc (GCC) 4.8.3 20140911</strong>, the output</p>
  
  <pre><code>ptest_s1-&gt;var1 = 1                                                            
  ptest_s1-&gt;var2 = 2
  </code></pre>
  
  <p>Will the behaviour be well-defined, according to the quotes from the standard given above?</p>