1

Now I know from this post about assigning one struct to another that I can assign a struct variable to another one of the same type and a shallow copy will happen.

struct Test t1;
struct Test t2;
t2 = t1;

But what if I do this?

struct Test *t1;
struct Test *t2;
t1 = malloc(sizeof(struct Test));
t2 = malloc(sizeof(struct Test));
//assign t1 and t2's fields some data
*t2 = *t1;

Will the same memcpy happen in this case?

Community
  • 1
  • 1
rubndsouza
  • 1,224
  • 15
  • 23

2 Answers2

3

the following

*t2 = *t1;

will indeed be doing a shallow copy. Basically, the * operators on pointers act as if you were using the pointed value.

But be sure to allocate memory and define values for those or you'll get an undefined behavior.

zmo
  • 24,463
  • 4
  • 54
  • 90
0

Yes, the types involved with both assignments are the same, so it follows that the effects of that assignment are also the same (assuming that t1 and t2 point to valid objects in the latter example).

dreamlax
  • 93,976
  • 29
  • 161
  • 209