Example code:
This appears to work fine (copying t1 into t2), until modifying t2, when t1 is modified as well since you have obviously only copied the pointer, rather than the data value.
I then pieced together a 'copy' method:
to replace 't2 = t1' with 'copy(t2, t1)', but this doesn't seem to work either. I think I'm just generally a bit confused, so can anyone poke my copy method in the correct direction?
Thanks,
Code:
int a, b, c; int *t1; int *t2; t1 = malloc(3*sizeof(int)); t1[0] = 1; t1[1] = 2; t1[2] = 3; t2 = malloc(3*sizeof(int)); t2 = t1; a = t2[0]; b = t2[1]; c = t2[2]; t2[0] = 5;
I then pieced together a 'copy' method:
Code:
void copy(int *to, int *from) { int c; for(c=0; c<sizeof(from)/sizeof(int); c++) { to[c] = from[c]; } }
Thanks,
Comment