What you want is the following:
memcpy(((char*)&testdata)+4, bufferwithstructdata, sizeof(PLAYERCOORDINATESSTRUCT)-4);
The reason is because the +
operator applied to pointers adds the integer specified times the size of the thing being pointed to. This is why adding 4 added 40 to the address. Casting to a char* before adding 4 will add 4 byte locations. Also, if you want something a bit more portable, the following would be a nice trick to do what you want:
memcpy(((int*)&testdata)+1, bufferwithstructdata, sizeof(PLAYERCOORDINATESSTRUCT)-4);
This has the benefit of adding one integer size unit to the address, regardless of the size of int
.
As Lalaland has pointed out, it would be even better to use offsetof like so:
memcpy(offsetof(PLAYERCOORDINATESSTRUCT, x), bufferwithstructdata, sizeof(PLAYERCOORDINATESSTRUCT)-4);