title: Pointers to struct and cast
body: <p>C language ensures that a pointer to any <code>struct</code> may be converted to <code>void *</code> and vice versa. Also, the language permits to define pointers to a <code>struct</code> also if it's undefined. I would assume that, since the compiler doesn't know anything about these structures, their pointers should have the same physical representation. Let's consider these lines into two separate modules:</p>
  
  <pre><code>/* FILE1.c */
  void *mem = ...; //Points to a suitable memory block
  struct s1 *p1; //No implementation given for struct s1
  void *mem2;
  
  p1 = (struct s1 *)mem;
  mem2 = &amp;p1;
  
  
  /* FILE2.c */
  extern void *mem2;
  struct s2 { /*...fields...*/ };
  struct s2 *p2;
  
  p2 = *(struct s2 **)mem2;
  </code></pre>
  
  <p>Is this code able to work on all the platforms, provided that the memory block is large enough to contain <code>struct s2</code> (e.g. allocated by <code>malloc(sizeof(struct s2))</code>)?</p>
  
  <p>In other words, is it correct (i.e. portable) to reinterpret a memory cell containing a pointer to <code>struct s1</code> structure as if it were a pointer to <code>struct s2</code>?</p>
  
  <p>(Disclaimer: I am perfectly aware that this is a very weird way to play with pointers, my question is theoretical)</p>