title: Binary Saving a Struct with Struct array members (C)?
body: <p>I'm currently working on a program that uses these two structs: </p>
  
  <pre><code>//Struct to hold contact info about a friend
  typedef struct
  {
      char *firstName, *lastName;
      char *home, *cell;
  } Friend;
  
  //Struct to hold a list of friends and keep track of its size.
  typedef struct
  {
      Friend *listEntries;
      size_t listSize;
  } FriendList;
  </code></pre>
  
  <p>Each of the string members within the <code>Friend</code> struct is dynamically allocated, as is the <code>listEntries</code> array inside <code>FriendList</code>. I'm trying to save a <code>FriendList</code> as a binary record, but when I try to do this using <code>fwrite()</code> and then read it back with <code>fread()</code>, the <code>listEntries</code> array of the <code>FriendList</code> that is read from the file is empty. Is there any way to save a dynamic struct like this?</p>
  
  <p><strong>EDIT: The functions I use to read/write the struct are:</strong></p>
  
  <pre><code>void writeList(FriendList *fl, char* filename)
  {
      FILE *fp = fopen(filename, "wb+");
  
      if(fp != NULL)
      {
          fwrite(fl, sizeof(FriendList), 1, fp);
      }
  
      fclose(fp);
  }
  
  void readList(FriendList *dest, char* filename)
  {
      FILE *fp = fopen(filename, "rb");
  
      if(fp == NULL)
      {
          printf("oops...");
      }
      else
      {
          fread(dest, sizeof(FriendList), 1, fp);
      }
  
      close(fp);
  }
  </code></pre>