, UB. , gcc, clang -pedantic.
C99 ( 6.5.2.3/6) : [1]
union OneTwo {
struct One one;
struct Two two;
};
OneTwo tmp = {.two = {3, {'a', 'b'}, 3.14f, 3.14159} };
One one = tmp.one;
, "" struct One union , , struct Two struct One. union, , union, union , struct.
, C ( , , C++) (& sect; 6.2.6.1/4) [2]. , :
struct One one;
struct Two two = ...;
unsigned char tmp[sizeof one];
memcpy(tmp, two, sizeof one);
memcpy(one, tmp, sizeof one);
void*, , , , :
struct One one;
struct Two two = ...;
unsigned char tmp[sizeof one];
memcpy(one, two, sizeof one);
, OP, , : , , a struct Two* a struct One*. (& sect; 6.3.2.3/7) [3], , , , . , , struct Two ( ) struct One - . :
one = *(struct One*)(void*)&two;
.
. , , . ; one two. , two , .
, , . , , , struct One*, struct Two , , , . , struct One, struct Two, - union.
--- :
[1] ", , (. ), , ."
[2] ", , n × CHAR_BIT
, n - , .
unsigned char [n] (, memcpy) & hellip; "
[3] " & hellip; , . , , ."