>Assume you can't change the code I posted
Code:
memcpy(buff[offset], mystruct, sizeof(mystruct));
But it's got bugs.
Code:
memcpy(&buff[offset], &mystruct, sizeof(mystruct));
You see, buff[offset] is a char, not a pointer; and mystruct is a struct, not a pointer. Since memcpy takes pointers for its first two arguments, if we leave it the way you posted, we'd likely to be writing to wherever from la-la land.
Code:
#include <stdio.h>
#include <string.h>
struct mytype
{
char name[20];
int i;
double d;
};
int main(void)
{
char buffer[128];
struct mytype myvar = { "name", -1, 3.14 }, *ptr;
int offset = 24;
ptr = (struct mytype*)&buffer[offset];
memcpy(ptr, &myvar, sizeof(myvar));
printf("ptr->name = \"%s\"\n", ptr->name);
printf("ptr->i = %d\n", ptr->i);
printf("ptr->d = %lf\n", ptr->d);
return 0;
}
/* my output
ptr->name = "name"
ptr->i = -1
ptr->d = 3.140000
*/