It is taking the address of the 18. byte and then reads a 4 byte long integer at that address, so it does read byte 19, 20 and 21 as well.
header
is a pointer to the place in memory where the BMP header is stored in bytes
header[18]
means the same as *(header + 18)
, we add 18 to the address and read the value at that address, tha *
operator so called dereferences the pointer
&header[18]
: take the address of that value, same as header + 18
, but some people find this form more expressive
(int*)&header[18]
: so far we worked with char*
pointers (pointers that point to memory that hold values type of char
), now we cast this pointer to an int
pointer, this doesn't modify anything in the memory, it just changes the way we interpret the data underlying the pointer
*(int*)&header[18]
: dereference the int*
to get the value of the integer at the header + 18
memory address, and since int
is 4 byte long on your platform, this will result in loading 4 bytes into the variable width
So we could simplify the expression like this: *(int*)(header + 18)
. This way its not as misleading and easy to be mistaken for only reading 1 bytes.
The reason we need to cast header
to an int*
is because the type of header
is char*
, dereferencing that would really only read a single byte.