title: How do I unpack and extract data properly using msgpack-c?
body: <p>I'm currently trying to use msgpack in a project written in C. I'm using msgpack for the purpose of serializing the contents of a struct, which is then to be sent over the network, and deserialized back into a corresponding struct on the other side.</p>
  
  <p>Condensed version of what I'm trying to do:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;msgpack.h&gt;
  #include &lt;stdbool.h&gt;
  
  typedef someStruct{
  uint32_t a;
  uint32_t b;
  float    c;
  } someStruct;
  
  int main (void){
      someStruct data;
      /* ... Fill 'data' with some data for test purposes ...*/
  
      msgpack_sbuffer* buff = msgpack_sbuffer_new();
      msgpack_packer* pck = msgpack_packer_new(buff, msgpack_sbuffer_write);        
  
      someStruct* structs = malloc(sizeof(someStruct) * 10);
  
      /* ... Fill 'structs' with members containing test data ... */
      // Serialize
      msgpack_pack_array (pck, 10);
  
      int i;
      for(i = 0 ; i &lt; 10 ; i++){
      msgpack_pack_array (pck, 3);
      msgpack_pack_uint32 (pck, structs[i].a);
      msgpack_pack_uint32 (pck, structs[i].b);
      msgpack_pack_float (pck, structs[i].c);
      }
  
      free(structs);
      msgpack_packer_free(pck);
  
      // Deserialize
      msgpack_unpacked msg;
      msgpack_unpacked_init(&amp;msg);
      bool deserialize_success = msgpack_unpack_next
                                 (&amp;msg, buff-&gt;data, buff-&gt;size, NULL);
      if(!deserialize_success) /* Error */
  
      msgpack_object obj = msg.data;
      msgpack_object_print(stdout,obj); // This seems to work perfectly, indicating serialize / deserialize works as intended...
  
      someStruct deserialized_data;
      /* Insert code to extract and cast deserialized data to 'deserialized_data */
      // Clean
      msgpack_sbuffer_free(buff);
      msgpack_packer_free(pck);
  
  return 0;
  }
  </code></pre>
  
  <p>The code listed is more or less ripped straight from <a href="http://wiki.msgpack.org/display/MSGPACK/QuickStart+for+C+Language" rel="noreferrer">here</a>, which seems to be one of very few resources on msgpack-c. </p>
  
  <p>Can anyone point me in the right direction as to a way to 'recreate' the original struct on the other side of the wire? The only way I've found to actually utilize the deserialized data, is to use the msgpack_object_print() call to print from the messagepack_object. This does, however seem to work, so I'm certain the data is there.</p>
  
  <p>Do I need to somehow loop through the serialized data and use msgpack_unpack_next() with an offset to retrieve each someStruct member? Using memcpy to a local byte buffer?</p>
  
  <p>Any help is greatly appreciated!</p>