0

The problem statement states that BMP files have a particular header with: (1) BITMAPFILEHEADER 14 bytes long and (2) BITMAPINFOHEADER 40 bytes long.

bmp.h (provided) includes the definitions of the structures such as for BITMAPINFOHEADER. I am thinking in terms of memory storage/allocation and trying to understand the following:

If BITMAPINFOHEADER is 40 bytes and the first 4 bytes correspond to the size, the following 4 bytes correspond to the Width, ... how do the fields point to the subset of bytes? How does the dot notation access memory addresses?

typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

typedef struct
{
DWORD  biSize;
LONG   biWidth;
LONG   biHeight;
WORD   biPlanes;
WORD   biBitCount;
DWORD  biCompression;
DWORD  biSizeImage;
LONG   biXPelsPerMeter;
LONG   biYPelsPerMeter;
DWORD  biClrUsed;
DWORD  biClrImportant;
} __attribute__((__packed__))
BITMAPINFOHEADER;

EDIT

CASE 2

typedef char* string;  
typedef struct
      {  
           string name;
           string number;
      }
      person;

      int main(void)
      {
      person people[2];
      people[0].name = "Brian";
      people[0].number= "+1-222-999-1000";
      }

1 Answer 1

1

The code knows how long each structure element is, thanks to the definition in the header file, so it only needs to add the appropriate number of bytes to the starting address to get to anything. The elements are sequentially ordered, and that order never changes, nor are any other data fields placed in the sequence.

For example, BITMAPINFOHEADER.biPlanes is at the starting address + 4 + 4 + 4 bytes offset from the starting address.

The offset is calculated when the code is compiled and hardcoded in machine code, while the starting address is set at run time.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Cliff @Cliff thanks for the answer. It makes sense in BITMAPINFOHEADER where if we declare two variables of type BITMAPINFOHEADER they will both be 40 bytes long with the ordered fields. However, what would happen in the case of strings where the offset may vary depending on the length of the string as shown in case 2. Thanks again. Commented Jun 5, 2021 at 2:40
  • @LorenzoPeve the offset will always be the same, because a pointer (which holds an address) has a fixed size (in the IDE, it is 8 bytes).
    – curiouskiwi
    Commented Jun 5, 2021 at 6:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .