Reputation: 1
I need to copy one instance of a structure to another, using the respective structure pointers. The code I have tried is as follows:
typedef struct{
int a, b, c;} test;
int main(){
test *q, *w;
(*w).a = 2;
(*w).b = 3;
(*w).c = 4;
printf("\n%d\n%d\n%d", (*w).a, (*w).b, (*w).c);
memcpy((void*)q, (void*)w, sizeof(test));
printf("\n%d\n%d\n%d", (*q).a, (*q).b, (*q).c);
return 0;
The output I get is:
2
3
4
1875984
32768
1296528
Can someone please tell me how to copy the structure? I need to use pointers to structures, simply doing:
test w, q;
q = w;
will not suffice for my program.
Thanks.
Upvotes: 0
Views: 44
Reputation: 4118
Replace the line in your code:
memcpy((void*)q, (void*)w, sizeof(test));
with the following line:
memcpy((void*)&q, (void*)&w, sizeof(test));
Upvotes: 2