title: Reading from BMP file into BMP header structures in C
body: <p>I'm trying to take a BMP file and read it in and then perform an operation on the pixels in it to change its color.  My problem is that I am having trouble reading in the data from the file into the two BMP header structures.  I'm able to read all the data into the first structure fine, but I get a seg fault when reading into the second.  As you can see from the code, the first structure, FILEHEADER, is read to and contains all the correct data it should, but the second struc, BMPInfoHeader, is not being read to correctly.  Why is this seg fault occuring?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct
  {   unsigned short int Type; /* Magic identifier */ 
      unsigned int Size; /* File size in bytes */ 
      unsigned short int Reserved1, Reserved2; 
      unsigned int Offset; /* Offset to data (in B) */
  }   FILEHEADER; /* 14 Bytes */
  
  typedef struct
  {   unsigned int Size; /* Header size in bytes */ 
      int Width, Height; /* Width / Height of image */ 
      unsigned short int Planes; /* Number of colour planes */ 
      unsigned short int Bits; /* Bits per pixel */ 
      unsigned int Compression; /* Compression type */ 
      unsigned int ImageSize; /* Image size in bytes */ 
      int xResolution, yResolution;/* Pixels per meter */ 
      unsigned int Colors; /* Number of colors */ 
      unsigned int ImportantColors;/* Important colors */ 
  } BMPInfoHeader;  /* 40 Bytes */
  
  typedef struct 
  {   unsigned char r; /* Red */
      unsigned char b; /* Blue */
      unsigned char g; /* Green */
  } IMAGE;
  
  int main(int argc, char *argv[]) {
  
      FILE *BMPFile;
      FILEHEADER BMPFileHeader;
      BMPInfoHeader *InfoHeader;
      BMPFile=fopen(argv[1],"rb");
      unsigned char *BMPimage;
  
      if (BMPFile==NULL) {
          printf("\n\nERROR: File not opened properly\n\n");
          return -1;
      }
  
      fread(&amp;BMPFileHeader,sizeof(unsigned char),14,BMPFile);
      fseek(BMPFile,BMPFileHeader.Offset,SEEK_SET);
      fread(InfoHeader,sizeof(unsigned char),40,BMPFile);
  
      if (BMPFileHeader.Type != 0x4D42) {
          printf("\n\nERROR with fread\n\n");
          return -1;
      }
  
          return 0;
   }
  </code></pre>