title: Writing a structure pointer into the file using fwrite
body: <p>I am trying to write the structure pointer to a file. What i am trying to to is to write a structure pointer to a character buffer, then retrieve the pointer from the buffer , and finally writing the whole structure to a file.</p>
  
  <p>Instead of writing the whole structure, I am getting junk data in the file.</p>
  
  <p>Am i missing something in the code or have made some mistake.</p>
  
  <p>Pasting my test code in full.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  # include&lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  typedef struct dummy_s {
  
    int a;
  } dummy_t;
  
  void main(int argc, char* argv)
  {
  
  
     char *s1;
     s1 = malloc(sizeof(dummy_t)*1000);
     dummy_t * tmp1;
     tmp1 = (dummy_t *)s1;
     tmp1 -&gt; a = 100;
     FILE *f;
     f = fopen("dummyfile.txt","w");
     dummy_t * tmp = NULL;
     tmp = malloc(sizeof(dummy_t));
     memcpy(tmp, s1, sizeof(dummy_t));
     fwrite(tmp,sizeof(struct dummy_s),1,f);
     fclose(f);
  }
  </code></pre>
  
  <p>[Edited for compilation errors - Sorry]
  cheers!
  Naren</p>