I am offsetting my pointer as shown in the below code to copy to another structure.
#include <stdio.h>
struct a
{
int i;
};
struct test
{
struct a *p;
int x,y,z;
};
int main()
{
struct test *ptr = malloc(sizeof(struct test));
struct test *q = malloc(sizeof(struct test));
ptr->x = 10;
ptr->y = 20;
ptr->z = 30;
memcpy(&(q->x),&(ptr->x),sizeof(struct test)-sizeof(struct a*));
printf("%d %d %d\n",q->x,q->y,q->z);
return 0;
}
Is there a better way to do my memcpy()
?
My question is what if I am in-cognizant of the members of the structure and want to just move my pointer by sizeof(struct a*)
and copy rest of the structure?
Edits:
I want to copy some part of the structure but I don't know the members in it, but I know I want to skip some type of variable as shown in the example (struct a*) and copy rest of the structure.