I’ve got a pointer to a struct with a union
so let’s say we have
struct A {
union {
char **word;
struct A *B
} u;
};
and I have variables x and y of type A*
typedef A* A_t;
A_t x;
A_t y;
would x->u = y->u be enough to copy over stuff in the union.
You cannot just dereference pointers if they are not pointing to anything valid.
To be able to do
x->uyou have to make surexpoints to some valid memory, The code you show dereferences an uninitialized pointer which causes an Undefined Behavior and most likely a crash. Same applies fory->u. So make surexandypoint to valid memory before you dereference them.Will not perform a deep copy but a shallow copy.
You will basically end up with two pointers pointing to the same memory, which is not probably what you intend or need.
If you need a deep copy, you should allocate your destination enough memory to hold the data being copied to it and then use
memcpyto copy the contents of the source union to it.Good Read:
What is the difference between a deep copy and a shallow copy?