title: Casting void pointers in C
body: <p>Let's assume a data structure in C such as</p>
  
  <pre><code>struct node {
      void *value;
      struct node *next;
  } *Node;
  </code></pre>
  
  <p><strong>Is there any chance of cast a void pointer to a specific type of value? If so, how to do it safely?</strong></p>
  
  <p>Creating a main like this</p>
  
  <pre><code>int main (void) {
      Node n = create_node ();
  
      insert (n, 2);
      insert (n, 3);
  
      return 0;
  }
  </code></pre>
  
  <p>Or this</p>
  
  <pre><code>int main (void) {
      Node n = creade_node ();
  
      // any struct. e.g.:
      struct data d = create_data ();
  
      insert (n, d);
  
      return 0;
  }
  </code></pre>
  
  <p>Makes compiler yell at me warnings like "expected ‘void *’ but argument is of type <em>some_type</em>. How can I solve this kind of warnings? </p>
  
  <p>Stuff like this (cast directly)</p>
  
  <pre><code>insert (Node n, void *value) {
      struct data new_value = (struct data) value;
      ...
  }
  </code></pre>
  
  <p>can be done? </p>
  
  <p>In Java, e.g., I can safely check if some method argument (object) is instance of a specific type and then perform a cast, if appropriate. How could I do this in C? </p>
  
  <h2><strong>Bottom line, is it possible to check if a void pointer (i.e., inside it) is of type <em>some_type</em> and if so, cast that value to <em>some_type</em>?</strong></h2>