title: Generic data type in C [ void * ]
body: <p>Hello I am trying to use void * as a generic data type in C. What I want is a mechanism using which I can store anything and get anything.  I wrote some code, but it is failing in the last case. Can anybody please take a look at the code, If you have any other idea please let me know.</p>
  
  <p>I know what I am trying to store, so at that point I know the data type, but during retrial I only know the starting address and size .</p>
  
  <p>Here is the code :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;assert.h&gt;
  
  
  static void store( void *destination, void *source, size_t size ) {
      memcpy ( (char*)destination, source, size);
  }
  static void retrieve ( void *destination, void *source, size_t size) {
      memcpy ( destination, (char*)source, size);
  }
  
  void *storage_char_ptr = (void*) malloc ( sizeof( char* ));
  void *storage_int_ptr  = (void*) malloc ( sizeof(int*));
  void *storage_int      = (void*) malloc ( sizeof( int));
  
  int main() {
  
      int int_in = 65;
      void *int_out_ptr;
      int *ptr = ( int*) malloc ( sizeof(int));
      memcpy ( ptr, &amp;int_in, sizeof(int));
  
      store ( storage_int_ptr, &amp;ptr, sizeof(int*));
      retrieve ( &amp;int_out_ptr, storage_int_ptr, sizeof(int*));
      assert ( int_in == *(int*)int_out_ptr);
  
      char *char_in = "HelloWorld!!";
      void *char_out;
      store ( storage_char_ptr, &amp;char_in, sizeof(char*));
      retrieve ( &amp;char_out, storage_char_ptr, sizeof(char*));
      assert ( strcmp ( char_in, (char*)char_out ) == 0 );
  
      char_in = _strdup("HelloWorld!!");
      store ( storage_char_ptr, &amp;char_in, sizeof(char*));
      retrieve ( &amp;char_out, storage_char_ptr, sizeof(char*));
      assert ( strcmp ( char_in, (char*)char_out ) == 0 );
  
      /* This is where it is failing */
      int_in = 55;
      void* int_out;
      store   ( storage_int, &amp;int_in, sizeof(int));
      retrieve ( &amp;int_out, storage_int, sizeof(int));
      assert  ( 55 == *(int*)int_out);
  
  } 
  </code></pre>