/** * Description: * Author: * Date: */ #include #include typedef struct { int m1i1; int m1i2; char* m1p1; } mess_1; typedef struct { int m2i1; int m2i2; long m2l1; } mess_2; typedef struct { int m_type; long m_source; union { mess_1 m_m1; mess_2 m_m2; } m_u; } message; 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->m_u.m_m1.m1i1, mptr->m_u.m_m1.m1i2, mptr->m_u.m_m1.m1p1); break; case 2: printf("m_type = %d\nm_source = %d\nm2i1 = %d\nm2i2 = %d\nm2l1 = %ld\n", mptr->m_type, mptr->m_source, mptr->m_u.m_m2.m2i1, mptr->m_u.m_m2.m2i2, mptr->m_u.m_m2.m2l1); break; default: ; } } int main(int argc, char *argv[]) { message m1 = { 1, 12, {{5, 10, "lock"}} }; message m2; /* Only the first member of a union can be initialized in the decl. */ m2.m_type = 2; m2.m_source = 14; m2.m_u.m_m2.m2i1 = 20; m2.m_u.m_m2.m2i2 = 30; m2.m_u.m_m2.m2l1 = 500L; print(&m1); print(&m2); printf("size of long = %d\n", sizeof(long)); return 0; }