title: Can integer operations (eg addition or subtraction) be performed on malloc buffers?
body: <p>To learn about JPEG compression, I downloaded a simple BMP/JPEG <a href="https://github.com/rockcarry/ffjpeg" rel="nofollow noreferrer">encoder</a> that I have been examining.  I have several questions, hopefully someone can help me with. I'm not a trained computer scientist, just a hobby programmer. Grateful for any help. </p>
  
  <p>The following code reads a bitmap, I left some information out for brevity. I understand that a bitmap header is written as a struct, so the components can be read directly to variables without iterating.  </p>
  
  <p>BYTE is a typedef for uint8_t</p>
  
  <pre><code>    int bmp_load(BMP *pb, char *file)
  {
      BMPFILEHEADER header = {0};
      FILE         *fp     = NULL;
      BYTE         *pdata  = NULL;
      int           i;
  
      fp = fopen(file, "rb");
      if (!fp) return -1;
  
      fread(&amp;header, sizeof(header), 1, fp);
      pb-&gt;width  = header.biWidth;
      pb-&gt;height = header.biHeight;
      pb-&gt;stride = ALIGN(header.biWidth * 3, 4);
      pb-&gt;pdata  = malloc(pb-&gt;stride * pb-&gt;height);// allocate a byte for each element of bitmap?
      if (pb-&gt;pdata) {
          pdata  = (BYTE*)pb-&gt;pdata + pb-&gt;stride * pb-&gt;height; // Title question: Cast pdata as uint8_t buffer but what is the addition for? I think it has to do with padding a BMP file.  Without addition there is a bus error: 10 
          for (i=0; i&lt;pb-&gt;height; i++) {
              pdata -= pb-&gt;stride; // What is the subtraction for? 
              fread(pdata, pb-&gt;stride, 1, fp);
          }
      }
  
      fclose(fp);
      return pb-&gt;pdata ? 0 : -1; // If pdata was being modified in the for loop, why is pb-&gt;pdata returned? 
  }
  
  </code></pre>
  
  <p>I have my questions as comments in the code and also pasted below. 
  What is the addition for? I think it has to do with padding a BMP file.  Without addition there is a bus error: 10 </p>
  
  <pre><code>    pdata  = (BYTE*)pb-&gt;pdata + pb-&gt;stride * pb-&gt;height;
  
  </code></pre>
  
  <p>What is the subtraction for? </p>
  
  <pre><code>    pdata -= pb-&gt;stride;
  </code></pre>
  
  <p>And finally, why is pb->pdata returned but its actually pdata that is modified in the code? </p>