title: C struct inheritance pointer alignment
body: <h1>Background</h1>
  
  <p>I have created a basic linked list data structure mainly for learning purposes. One goal of the list was that it can handle different data structures. Therefore, I've tried my hand at structure composition to simulate "inheritance" in C. Here are the structures that form the basis for my linked list.</p>
  
  <pre><code>typedef struct Link {
      struct Link* next;
      struct Link* prev;
  } Link;
  
  typedef Link List;
  </code></pre>
  
  <p>In my implementation I have chosen to have a sentinel node that serves as both the head and the tail of the list (which is why Link == List).</p>
  
  <p>To make the list actually handle data, a structure simply includes the Link structure as the first member:</p>
  
  <pre><code>typedef struct {
      Link link;
      float data;
  } Node;
  </code></pre>
  
  <p>So the linked list looks like this.</p>
  
  <pre><code>         ┌───┬───┬───┐     ┌───┬───┐     ┌───┬───┬───┐     
  ... &lt;---&gt;│ P │ N │ D │&lt;---&gt;│ P │ N │&lt;---&gt;│ P │ N │ D │&lt;---&gt; ... 
           └───┴───┴───┘     └───┴───┘     └───┴───┴───┘
           End Node          myList        First Node
  
  List myList;
  Node node1 = {{}, 1.024};
  ....
  Node nodeN = {{}, 3.14};
  
  list_init(&amp;myList) // myList.next = &amp;myList; myList.prev = &amp;myList;
  list_append(&amp;myList, &amp;node1);
  ....
  list_append(&amp;myList, &amp;nodeN);
  </code></pre>
  
  <h1>Question</h1>
  
  <p>To traverse this list a <code>Node</code> pointer initially points to the <em>First Node</em>. It then traverses along the list until it points to the sentinel again then stops.</p>
  
  <pre><code>void traverse()
  {
      Node* ptr;
      for(ptr = myList.next; ptr != &amp;myList; ptr = ptr-&gt;link.next)
      {
          printf("%f ", ptr-&gt;data);
      }
  }
  </code></pre>
  
  <p>My question is with the line <code>ptr != &amp;myList</code>. <strong>Is there a pointer alignment problem with this line?</strong></p>
  
  <p>The for loop correctly produces the warnings: (<code>warning: assignment from incompatible pointer type</code> and <code>warning: comparison of distinct pointer types lacks a cast</code>) which can be silenced by doing what it says and casting to <code>Node*</code>. However, is this a DumbThingToDo™? I am never accessing <code>ptr-&gt;data</code> when it points to <code>&amp;myList</code> as the loop terminates once <code>ptr == &amp;myList</code>.</p>
  
  <h1>TLDR</h1>
  
  <p>In C structs, a <code>Base*</code> can point to a <code>Derived</code> if <code>Base</code> is the first member in <code>Derived</code>. Can a <code>Derived*</code> point to a <code>Base</code> if none of <code>Derived</code> specific members are accessed?</p>
  
  <p><em>EDIT</em>: replaced relevant function calls with their equivalent inline code. </p>