/** * Description: * Author: * Date: */ #include #include typedef struct { int m1i1; int m1i2; char* m1p1; } mess_1; typedef struct { int m2i1; int m2i2; long int m2l1; } mess_2; typedef struct { int m_type; long m_source; union { mess_1 m_m1; mess_2 m_m2; } m_u; } message; #define m1_i1 m_u.m_m1.m1i1 #define m1_i2 m_u.m_m1.m1i2 #define m1_p1 m_u.m_m1.m1p1 #define m2_i1 m_u.m_m2.m2i1 #define m2_i2 m_u.m_m2.m2i2 #define m2_l1 m_u.m_m2.m2l1 void print(message *mptr) { printf("\nMessage type: %d\n", mptr->m_type); switch( mptr->m_type ) { case 1: printf("m_type = %d\nm_source = %ld\nm1i1 = %d\nm1i2 = %d\n*m1p1 = %s\n", mptr->m_type, mptr->m_source, mptr->m1_i1, mptr->m1_i2, mptr->m1_p1); break; case 2: printf("m_type = %d\nm_source = %ld\nm2i1 = %d\nm2i2 = %d\nm2l1 = %ld\n", mptr->m_type, mptr->m_source, mptr->m2_i1, mptr->m2_i2, mptr->m2_l1); break; default: ; } } int main(int argc, char *argv[]) { message m1 = { 1, 12, {{5, 10, "lock"}} }; message m2 = { 2, 14, {{20, 30, 500L}} }; message m; /* m2.m_type = 2; m2.m_source = 14; m2.m2_i1 = 20; m2.m2_i2 = 30; m2.m2_l1 = 500L; */ printf("\n\tNote: size of int = %d and size of long = %d\n\n", sizeof(int), sizeof(long)); m = m1; print(&m); m = m2; print(&m); return 0; }