title: Casting struct pointers
body: <p>Assuming code is compiled with c11 and strict aliasing enabled.</p>
  
  <p>I am not searching for a different approach, I would like to focus on this specific problem and if it works or why not.</p>
  
  <p>(If I unintentionally made some unrelated error let me know and I will fix it)</p>
  
  <p>c11 standard says:</p>
  
  <blockquote>
    <p>6.2.5.28 
    All pointers to structure types shall have the same representation and alignment requirements as each other.</p>
    
    <p>6.7.2.1.6
    a structure is a type consisting of a sequence of members, whose
    storage is allocated in an ordered sequence</p>
  </blockquote>
  
  <p>This means the pointer size and alignment of pointers in struct A and B are the same.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct S1
  {
      int i ;
  } ;
  
  struct S2
  {
      float f ;
  } ;
  
  struct A
  {
      struct S1* p ;
  } ;
  
  
  struct B
  {
      struct S2* p ;
  } ;
  
  
  int main( void )
  {
  </code></pre>
  
  <p>Structs A and B have pointers to structs S1 and S2, and structs A and B are guaranteed to have the same size and alignment.</p>
  
  <p>We have a <code>struct B</code> whose member pointer is a struct S2 pointer, but is pointing to some struct S1, which achieved with a void* cast.</p>
  
  <pre><code>struct S1 s1 = { 0 } ;
  
  struct B* b = malloc( sizeof( *b ) ) ;
  b-&gt;p = ( void* ) &amp;s1 ;
  </code></pre>
  
  <p>That is ok, we can store the pointer, as long as we don't  actually use the pointer.
  But we want to.
  We could cast the pointer to struct S1.</p>
  
  <pre><code>( ( struct S1* )(b-&gt;p) )-&gt;i = 123 ;    //redundant brackets for emphasis
  
  printf("%d\n" , s1.i ) ;
  </code></pre>
  
  <p>And use it correctly.</p>
  
  <p>So far I don't see any problems, as the pointer was casted to the correct type.</p>
  
  <p>But can we cast the whole struct B to struct A instead? They are the same regarding size and alignment, though the standard might complain(?), could compilers produce undefined behavior?</p>
  
  <pre><code>( ( struct A* )b)-&gt;p-&gt;i = 666 ;
  
  printf("%d\n" , s1.i ) ;
  </code></pre>
  
  <p>I know the solution is to use an union( or use a void and just cast correctly any time), as the standard allows to use the member   not last used to store a value.</p>
  
  <blockquote>
    <p>6.5.2.3.3( 95 )
    If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.</p>
  </blockquote>
  
  <p>But, I would like to avoid this:</p>
  
  <pre><code>struct C
  {
      union
      {
          struct S1* p1 ;
          struct S2* p2 ;
      } ;
  } ;
  
  struct C* c = malloc( sizeof( *c ) ) ;
  
  c-&gt;p2 = ( void* )&amp;s1 ;
  
  c-&gt;p1-&gt;i = 444 ;
  
  printf("%d\n" , s1.i ) ;
  
  return 0 ;
  }
  </code></pre>
  
  <p><a href="http://pastebin.com/F93hfq63" rel="nofollow">Above code without text</a>.</p>