Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- union
- {
- int x;
- float y;
- } u;
- // Convert floating-point bits to integer:
- u.f = 3.14159f;
- printf("As integer: %08xn", u.x);
- enum Type { INTS, FLOATS, DOUBLE };
- struct S
- {
- Type s_type;
- union
- {
- int s_ints[2];
- float s_floats[2];
- double s_double;
- };
- };
- void do_something(struct S *s)
- {
- switch(s->s_type)
- {
- case INTS: // do something with s->s_ints
- break;
- case FLOATS: // do something with s->s_floats
- break;
- case DOUBLE: // do something with s->s_double
- break;
- }
- }
- typedef union
- {
- struct {
- unsigned char byte1;
- unsigned char byte2;
- unsigned char byte3;
- unsigned char byte4;
- } bytes;
- unsigned int dword;
- } HW_Register;
- HW_Register reg;
- reg.dword = 0x12345678;
- reg.bytes.byte3 = 4;
- typedef union
- {
- struct {
- unsigned char b1:1;
- unsigned char b2:1;
- unsigned char b3:1;
- unsigned char b4:1;
- unsigned char reserved:4;
- } bits;
- unsigned char byte;
- } HW_RegisterB;
- HW_RegisterB reg;
- x = reg.bits.b2;
- union {
- int a;
- int b;
- int c;
- } myUnion;
- Connection
- / |
- Network USB VirtualConnection
- struct Connection
- {
- int type;
- union
- {
- struct Network network;
- struct USB usb;
- struct Virtual virtual;
- }
- };
- typedef union {
- unsigned char control_byte;
- struct {
- unsigned int nibble : 4;
- unsigned int nmi : 1;
- unsigned int enabled : 1;
- unsigned int fired : 1;
- unsigned int control : 1;
- }
- } ControlRegister;
- typedef union MyUnion MYUNION;
- union MyUnion
- {
- int MyInt;
- float MyFloat;
- };
- MYUNION u;
- u.MyInt = 10;
- u.MyFloat = 1.0;
- set a to b times 7.
- #define ELEM_SYM_SET 0
- #define ELEM_SYM_TO 1
- #define ELEM_SYM_TIMES 2
- #define ELEM_SYM_FULLSTOP 3
- #define ELEM_VARIABLE 100
- #define ELEM_CONSTANT 101
- typedef struct {
- int elem_type;
- union {
- char *elem_str;
- int elem_val;
- }
- } tElem;
- tElem e;
- e.elem_type = ELEM_SYM_SET;
- tElem e;
- e.elem_type = ELEM_VARIABLE;
- e.elem_str = strdup ("b"); // make sure you free this later
- tElem e;
- e.elem_type = ELEM_CONSTANT;
- e.elem_val = 7; // make sure you free this later
- +------------------------+
- 0x1010 | |
- 0x1011 | elem_type |
- 0x1012 | |
- 0x1013 | |
- +-------------+----------+
- 0x1014 | | |
- 0x1015 | elem_string | elem_val |
- 0x1016 | | |
- 0x1017 | | |
- +-------------+----------+
- +-------------+
- 0x1010 | |
- 0x1011 | elem_type |
- 0x1012 | |
- 0x1013 | |
- +-------------+
- 0x1014 | |
- 0x1015 | elem_string |
- 0x1016 | |
- 0x1017 | |
- +-------------+
- 0x1018 | |
- 0x1019 | elem_val |
- 0x101A | |
- 0x101B | |
- +-------------+
- struct variant {
- int type;
- double number;
- char *string;
- };
- struct variant {
- int type;
- double number;
- char *string;
- };
- struct variant {
- int type;
- union {
- double number;
- char *string;
- } value;
- };
- struct _mydata {
- int which_one;
- union _data {
- int a;
- float b;
- char c;
- } foo;
- } bar;
- switch (bar.which_one)
- {
- case INTEGER : /* access bar.foo.a;*/ break;
- case FLOATING : /* access bar.foo.a;*/ break;
- case CHARACTER: /* access bar.foo.a;*/ break;
- }
- typedef union
- {
- unsigned char color[4];
- int new_color;
- } u_color;
- union data {
- int data;
- struct {
- unsigned char higher;
- unsigned char lower;
- } parts;
- };
- union _Obj {
- union _Obj* _M_free_list_link;
- char _M_client_data[1]; /* The client sees this. */
- };
Add Comment
Please, Sign In to add comment