title: Writing to a bmp file in C
body: <p>I am having difficulties when writing to a bmp file.
  Here is the code where the matrix is allocated:</p>
  
  <pre><code>  // Dynamically allocate matrix for the bitmap and 
    // while doing so, color it in blue.
    Pixel **bmp_matrix = (Pixel**)malloc(config.resolution_height_ * 
                                         sizeof(Pixel*));
    if (bmp_matrix == NULL)
    {
      printf("error: out of memory\n");
      return MEMORY_ERROR;
    }
    int rows = 0;
    for(; rows &lt; config.resolution_height_; rows++)
    {
      bmp_matrix[rows] = (Pixel*)malloc(config.resolution_width_ * 
                                        sizeof(Pixel));
      if (bmp_matrix[rows] == NULL)
      {
        while(--rows &gt;= 0)
          free(bmp_matrix[rows]);
        free(bmp_matrix);
        printf("error: out of memory\n");
        return MEMORY_ERROR;
      }
      int columns = 0;
      for(; columns &lt; config.resolution_width_; columns++)
        {
          bmp_matrix[rows][columns].red_ = 175;
          bmp_matrix[rows][columns].green_ = 175;
          bmp_matrix[rows][columns].blue_ = 255;
        }
    }
  </code></pre>
  
  <p>Here is the code for writing the pixels:</p>
  
  <pre><code>int height, width, pad_iterator;
    for(height = info_header.height_ - 1; height &gt;= 0; height--)
    {
      // for(width = 0; width &lt; info_header.width_; width++)
      //   fwrite(&amp;bmp_matrix[height][width], sizeof(Pixel), 1, bitmap_file);
      fwrite(&amp;bmp_matrix[height], sizeof(Pixel), info_header.width_, bitmap_file);
      for(pad_iterator = 0; pad_iterator &lt; pad_length; pad_iterator++)
        fwrite(&amp;pad, sizeof(Byte), 1, bitmap_file);
    }
  </code></pre>
  
  <p>Now, when I use the <code>for</code> loop which is commented, everything works perfectly. The resulting image is OK.
  However, I am trying to substitute it with a single <code>fwrite</code> so the program would not have to iterate the loop.
  In this case the resulting image is completely wrong.</p>
  
  <p>Any help?</p>