Printing a structure in C
<p>I am making some changes in wireshark source code. At one particular point, I print out a structure called <code>SSLDecoder</code> like this </p>

<pre><code>FILE *f;  
f=fopen("file.txt","a+");
size_decoder=sizeof(SslDecoder);  
for(i=0; i &lt; size_decoder; i++){
    fprintf(f,"%02x",*(ssl_session-&gt;client_new+i));
}
fprintf(f,"\n");
</code></pre>

<p><code>ssl_session</code> is of type <code>struct SslDecryptSession</code> which is defined as </p>

<pre><code>typedef struct _SslDecryptSession {
    guchar _master_secret[SSL_MASTER_SECRET_LENGTH];
    guchar _session_id[256];
    guchar _client_random[32];
    guchar _server_random[32];
    StringInfo session_id;
    StringInfo session_ticket;
    StringInfo server_random;
    StringInfo client_random;
    StringInfo master_secret;
    StringInfo handshake_data;
    /* the data store for this StringInfo must be allocated explicitly with a capture lifetime scope */
    StringInfo pre_master_secret;
    guchar _server_data_for_iv[24];
    StringInfo server_data_for_iv;
    guchar _client_data_for_iv[24];
    StringInfo client_data_for_iv;

    gint state;
    SslCipherSuite cipher_suite;
    SslDecoder *server;
    SslDecoder *client;
    SslDecoder *server_new;
    SslDecoder *client_new;
    gcry_sexp_t private_key;
    StringInfo psk;
    guint16 version_netorder;
    StringInfo app_data_segment;
    SslSession session;
} SslDecryptSession;
</code></pre>

<p><code>SslDecoder</code> is of type </p>

<pre><code>typedef struct _SslDecoder {
    SslCipherSuite* cipher_suite;
    gint compression;
    guchar _mac_key_or_write_iv[48];
    StringInfo mac_key; /* for block and stream ciphers */
    StringInfo write_iv; /* for AEAD ciphers (at least GCM, CCM) */
    SSL_CIPHER_CTX evp;
    SslDecompress *decomp;
    guint32 seq;
    guint16 epoch;
    SslFlow *flow;
} SslDecoder;
</code></pre>

<p>However, the output printed to the file is irregular.
Sample output is here on <a href="http://pastebin.com/avdVXPyM" rel="nofollow">pastebin</a></p>

<p>Can someone please tell me what I am doing wrong ?</p>

<p>I later use this output in the file to populate another decoder like this (Right now as it is, I face a seg fault because of the irregular print to the file)</p>

<pre><code>SslDecoder *temp_decoder;
c = malloc(sizeof(SslDecoder));

for (i=0; i&lt;sizeof(SslDecoder); i++){
    fscanf(f,"%02x",&amp;c[i]);
}
memcpy(temp_decoder,c, sizeof(SslDecoder));
</code></pre>