I have a function
static void writeBytes(char ** pDestStr, char* item) {
int size=strlen(item); // expected size==2
memcpy(*pDestStr,item,size);
*pDestStr+=size;
}
and a 2 byte string defined in struct HEADER
typedef struct HEADER {
unsigned char magicNo[2]; // two bytes
} BMPHEADER;
In my program the magicNo
is set to 2 bytes and it contains "BM". But when I call the function like this:
writeBytes(&pBlock, bmpfile->header.magicNo);
inside writeBytes
there is sizeof item == 4 bytes and sizeof *item == 1 byte,
hence I ask, why this happen? The string contains 2 additional characters which I did not set. This is on the first line of the function definition. As a result the function works wrong because it adds the pointer +4 instead +2 bytes... Any idea how to fix this?
Solved: Answered in comments. Correct size of magicNo should be 3 and I am missing terminating character \0. So correction:
unsigned char magicNo[3];