code_junkie

Unions in C

Nov 12th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. union
  2. {
  3. int x;
  4. float y;
  5. } u;
  6.  
  7. // Convert floating-point bits to integer:
  8. u.f = 3.14159f;
  9. printf("As integer: %08xn", u.x);
  10.  
  11. enum Type { INTS, FLOATS, DOUBLE };
  12. struct S
  13. {
  14. Type s_type;
  15. union
  16. {
  17. int s_ints[2];
  18. float s_floats[2];
  19. double s_double;
  20. };
  21. };
  22.  
  23. void do_something(struct S *s)
  24. {
  25. switch(s->s_type)
  26. {
  27. case INTS: // do something with s->s_ints
  28. break;
  29.  
  30. case FLOATS: // do something with s->s_floats
  31. break;
  32.  
  33. case DOUBLE: // do something with s->s_double
  34. break;
  35. }
  36. }
  37.  
  38. typedef union
  39. {
  40. struct {
  41. unsigned char byte1;
  42. unsigned char byte2;
  43. unsigned char byte3;
  44. unsigned char byte4;
  45. } bytes;
  46. unsigned int dword;
  47. } HW_Register;
  48. HW_Register reg;
  49.  
  50. reg.dword = 0x12345678;
  51. reg.bytes.byte3 = 4;
  52.  
  53. typedef union
  54. {
  55. struct {
  56. unsigned char b1:1;
  57. unsigned char b2:1;
  58. unsigned char b3:1;
  59. unsigned char b4:1;
  60. unsigned char reserved:4;
  61. } bits;
  62. unsigned char byte;
  63. } HW_RegisterB;
  64. HW_RegisterB reg;
  65.  
  66. x = reg.bits.b2;
  67.  
  68. union {
  69. int a;
  70. int b;
  71. int c;
  72. } myUnion;
  73.  
  74. Connection
  75. / |
  76. Network USB VirtualConnection
  77.  
  78. struct Connection
  79. {
  80. int type;
  81. union
  82. {
  83. struct Network network;
  84. struct USB usb;
  85. struct Virtual virtual;
  86. }
  87. };
  88.  
  89. typedef union {
  90. unsigned char control_byte;
  91. struct {
  92. unsigned int nibble : 4;
  93. unsigned int nmi : 1;
  94. unsigned int enabled : 1;
  95. unsigned int fired : 1;
  96. unsigned int control : 1;
  97. }
  98. } ControlRegister;
  99.  
  100. typedef union MyUnion MYUNION;
  101. union MyUnion
  102. {
  103. int MyInt;
  104. float MyFloat;
  105. };
  106.  
  107. MYUNION u;
  108. u.MyInt = 10;
  109.  
  110. u.MyFloat = 1.0;
  111.  
  112. set a to b times 7.
  113.  
  114. #define ELEM_SYM_SET 0
  115. #define ELEM_SYM_TO 1
  116. #define ELEM_SYM_TIMES 2
  117. #define ELEM_SYM_FULLSTOP 3
  118. #define ELEM_VARIABLE 100
  119. #define ELEM_CONSTANT 101
  120.  
  121. typedef struct {
  122. int elem_type;
  123. union {
  124. char *elem_str;
  125. int elem_val;
  126. }
  127. } tElem;
  128.  
  129. tElem e;
  130. e.elem_type = ELEM_SYM_SET;
  131.  
  132. tElem e;
  133. e.elem_type = ELEM_VARIABLE;
  134. e.elem_str = strdup ("b"); // make sure you free this later
  135.  
  136. tElem e;
  137. e.elem_type = ELEM_CONSTANT;
  138. e.elem_val = 7; // make sure you free this later
  139.  
  140. +------------------------+
  141. 0x1010 | |
  142. 0x1011 | elem_type |
  143. 0x1012 | |
  144. 0x1013 | |
  145. +-------------+----------+
  146. 0x1014 | | |
  147. 0x1015 | elem_string | elem_val |
  148. 0x1016 | | |
  149. 0x1017 | | |
  150. +-------------+----------+
  151.  
  152. +-------------+
  153. 0x1010 | |
  154. 0x1011 | elem_type |
  155. 0x1012 | |
  156. 0x1013 | |
  157. +-------------+
  158. 0x1014 | |
  159. 0x1015 | elem_string |
  160. 0x1016 | |
  161. 0x1017 | |
  162. +-------------+
  163. 0x1018 | |
  164. 0x1019 | elem_val |
  165. 0x101A | |
  166. 0x101B | |
  167. +-------------+
  168.  
  169. struct variant {
  170. int type;
  171. double number;
  172. char *string;
  173. };
  174.  
  175. struct variant {
  176. int type;
  177. double number;
  178. char *string;
  179. };
  180.  
  181. struct variant {
  182. int type;
  183. union {
  184. double number;
  185. char *string;
  186. } value;
  187. };
  188.  
  189. struct _mydata {
  190. int which_one;
  191. union _data {
  192. int a;
  193. float b;
  194. char c;
  195. } foo;
  196. } bar;
  197.  
  198. switch (bar.which_one)
  199. {
  200. case INTEGER : /* access bar.foo.a;*/ break;
  201. case FLOATING : /* access bar.foo.a;*/ break;
  202. case CHARACTER: /* access bar.foo.a;*/ break;
  203. }
  204.  
  205. typedef union
  206. {
  207. unsigned char color[4];
  208. int new_color;
  209. } u_color;
  210.  
  211. union data {
  212. int data;
  213. struct {
  214. unsigned char higher;
  215. unsigned char lower;
  216. } parts;
  217. };
  218.  
  219. union _Obj {
  220. union _Obj* _M_free_list_link;
  221. char _M_client_data[1]; /* The client sees this. */
  222. };
Add Comment
Please, Sign In to add comment