title: C read bmp pixels into 2d array
body: <p>I've got the header and info header. the first pixel reads ok with the following code.</p>
  
  <pre><code>// The BMPHEADER structure.
  typedef struct {
  byte        sigB;
  byte        sigM;
  int32_t     fileSize;
  int16_t     resv1;
  int16_t     resv2;
  int32_t     pixelOffset;
  } tBmpHeader;
  
  // The BMPINFOHEADER structure.
  typedef struct {
  int32_t     size;
  int32_t     width;
  int32_t     height;
  int16_t     colorPlanes;
  int16_t     bitsPerPixel;
  byte        zeros[24];
  } tBmpInfoHeader;
  
  typedef uint8_t byte;
  
  typedef struct {
  byte blue;
  byte green;
  byte red;
  } tPixel;
  
  // A BMP image consists of the BMPHEADER and BMPINFOHEADER structures, and the 2D pixel array.
  typedef struct {
  tBmpHeader      header;
  tBmpInfoHeader  infoHeader;
  tPixel          **pixel;
  } tBmp;
  
  tPixel **BmpPixelAlloc(int pWidth, int pHeight)
  {
      tPixel **pixels = (tPixel **)malloc (pHeight * sizeof(tPixel *));
      for (int row = 0; row &lt; pHeight; ++row)
      {
          pixels[row] = (tPixel *)malloc(pWidth * sizeof(tPixel));
      }
  
      return pixels;
  }
  
  tError BmpRead(char *pFilename, tBmp *pBmp)
  {
  pBmp-&gt;pixel = BmpPixelAlloc(pBmp-&gt;infoHeader.width, pBmp-&gt;infoHeader.height);
  
  if(FileRead(file, &amp;pBmp-&gt;pixel, sizeof(tPixel), 1)!=0)
  { 
      errorCode = ErrorFileRead;
  }
  }
  </code></pre>
  
  <p>a tutor I was talking to said I should do the following</p>
  
  <pre><code>int i = 0;
  while(!feof(file))
  {
      if(FileRead(file, &amp;pBmp-&gt;pixel[i], sizeof(tPixel), 1)!=0)
      { 
      errorCode = ErrorFileRead;
      }
      ++i;
  }
  </code></pre>
  
  <p>but this gives me a segmentation fault. if i put a print in the loop it prints a few thousand times before saying segmentation fault. I tried using a double for loop but that wouldn't even compile. I've spent a few hours on google but cant figure it out.</p>