title: C: fread returning 0
body: <p>)
  I am having a problem with fread, and have spent 2 days trying to figure out what's wrong with this. The problem is one of my freads is returning 0..when it shouldn't.</p>
  
  <p>I have a struct such as</p>
  
  <pre><code>typedef struct
  {
      contents
  } tM;
  </code></pre>
  
  <p>I have a pointer of type tM:</p>
  
  <pre><code>tM *macs = NULL;
  </code></pre>
  
  <p>that pointer is dynamically allocated with realloc. It stores contents about machines, such as <code>macs[1].machineName = blabla</code>. Each time I "add a machine" with realloc, I increase the machineCounter.</p>
  
  <p>I have a function which saves this to a file, and one which reads from it. I don't know which one is malfunctioning, but something's wrong.</p>
  
  <p>I save this to a file this way:</p>
  
  <pre><code>void writeMac(int countMac, tM *macs)
  {
      FILE *fileMacs;
      if (countMac != 0)
      {
          fileMacs = fopen("machines.dat","wb");
          if (fileMacs == NULL)
          {
              errorcodehere
          }
          else
          {
              fwrite(&amp;countMac,sizeof(int),1,fileMacs);
              fwrite(macs,sizeof(tM),countMac,fileMacs);
              fclose(fileMacs);
          }
      }
  }
  </code></pre>
  
  <p>And the file is created with a size other than 0, but I don't know if that is reason enough for me to believe the file is created properly.</p>
  
  <p>And reading from that file:</p>
  
  <pre><code>void readMacs(int *countMac, tM *macs)
  {
      FILE *fileMacs;
      int numberMachinesRead;
      fileMacs = fopen("machines.dat","rb");
      if (fileMacs == NULL)
      {
          errorcontenthere
      }
      else
      {
          fread(&amp;(*countMac),sizeof(int),1,fileMacs);
          numberMachinesRead = fread(macs, sizeof(tM), *countMac, fileMacs);
          fclose(fileMacs);
      }
  }
  </code></pre>
  
  <p>The problem is that when debugging the code, while reading, the first fread does assign to *countMac the correct ammount of machines, however, the 2nd fread returns 0 always.
  Even though *countMac > 0, it's returning 0.
  What's wrong? Should fread be done differently considering macs is a pointer?
  Thank you in advance!</p>