0
Is it possible to copy a structure to a structure pointer?
3 Antworten
0
Tried copying structure pointer to structure which is possible while the reverse is not possible: Can someone help out? And so missed the structure names...(included them now)
However while trying to copy ptr to rcb1 in the copy function, could see that the variables(a,b) in rcb1 point to some other value (probably address?)
typedef struct test {
int a;
int b;
} test1_t;
typedef struct core {
int c;
test1_t t;
} core_t;
typedef struct rcb {
int c;
test1_t *t;
} rcb_t;
void copy (rcb_t *rcb1, core_t *ptr);
int main() {
core_t *ptr;
rcb_t *rcb;
rcb_t *rcb1;
rcb_t r,r1;
r.c = 5;
r1.c = 0;
r.t = (test1_t *) malloc(sizeof(test1_t));
r1.t = (test1_t *) malloc(sizeof(test1_t));
r.t->b = 6;
r.t->a = 4;
r1.t->a = 8;
r1.t->b = 9;
rcb = &r;
ptr->c = rcb->c;
if(rcb->t) {
memcpy(&ptr->t, rcb->t, sizeof(test1_t));
}
rcb = &r1;
copy(rcb1,ptr);
return 0;
}
void copy (rcb_t *rcb1, core_t *ptr) {
rcb->c = ptr->c;
memcpy(rcb1->t->b, &(ptr->t.b),sizeof(test1_t));
}
0
Correcting few more errors:
void copy (rcb_t *rcb1, core_t *ptr);
int main() {
core_t *ptr;
rcb_t *rcb;
rcb_t *rcb1;
rcb_t r,r1;
r.c = 5;
r1.c = 0;
r.t = (test1_t *) malloc(sizeof(test1_t));
r1.t = (test1_t *) malloc(sizeof(test1_t));
r.t->b = 6;
r.t->a = 4;
r1.t->a = 8;
r1.t->b = 9;
rcb = &r;
ptr->c = rcb->c;
if(rcb->t) {
memcpy(&ptr->t, rcb->t, sizeof(test1_t));
}
rcb1 = &r1;
copy(rcb1,ptr);
return 0;
}
void copy (rcb_t *rcb1, core_t *ptr) {
rcb1->c = ptr->c;
memcpy(rcb1->t->b, &(ptr->t.b),sizeof(test1_t));
}