title: Dereferencing void pointers
body: <p>In the hope of gaining a better understanding of the answers
  given in <a href="https://stackoverflow.com/questions/13618012/creating-a-generic-circular-buffer">this</a> post, can someone please explain to me if the
  following circular buffer implementation is possible, and if
  not, why not. </p>
  
  <pre><code>#define CB_TYPE_CHAR     0
  #define CB_TYPE_FLOAT    1
  
  ...
  
  typedef struct CBUFF
  {
      uint16 total;       /* Total number of array elements */
      uint16 size;        /* Size of each array element */
      uint16 type;        /* Array element type */
      uint16 used;        /* Number of array elements in use */
      uint16 start;       /* Array index of first unread element */
      void *elements;     /* Pointer to array of elements */
  } CBUFF;
  
  ...
  
  void cbRead(CBUFF *buffer, void *element)
  {
      if (buffer-&gt;type == CB_TYPE_CHAR)
      {
      /* The RHS of this statement is the problem */
          *(char*)element = *(buffer-&gt;elements[buffer-&gt;start]);
      }
  
      /* Other cases will go here */
  
      buffer-&gt;start = (buffer-&gt;start + 1) % buffer-&gt;total;
  
      --buffer-&gt;used;
  }
  </code></pre>
  
  <p>I understand that the LHS must be cast to char so that I can
  dereference the void pointer. I also understand that this code
  fragment:</p>
  
  <pre><code>buffer-&gt;elements[buffer-&gt;start]
  </code></pre>
  
  <p>gives the address of the 'buffer->start' element of the elements
  array, which I also want to dereference in order to get to the
  content of that address. Or at least that's what I take from
  K&amp;R.</p>
  
  <p>Given all that, how do I tell the compiler that the content of
  the memory at that address is a char, and that it is okay to
  dereference it? There is something going on here I just don't
  understand.</p>