Don't do this. Copy the fields manually:
struct b p;
p.u = k.a;
p.w = k.b;
p.data = (long *) k.c;
Your code would most likely run just fine since long *
and void *
will typically be identically implemented, but why risk it? The standard says stuff like:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
Note the last sentence, where pointers to "other types" (such as long
) are made distinct from pointers to void
.
The above is explicit, safe, and much clearer.