title: Working with a union of structs in C
body: <p>Say I have the following types:</p>
  
  <pre><code>typedef struct TYPEA
  {
      int type;
      char[12] id;
  } TYPEA;
  
  typedef struct TYPEB
  {
      int type;
      int value;
  } TYPEB;
  </code></pre>
  
  <p>I want to use create a union of these types and 'int', so that I can access the "type" int without needing to know whether TYPEA or TYPEB is stored in the union (the value of int lets me determine which is actually stored there). I can't get the right syntax though.</p>
  
  <p>My union:</p>
  
  <pre><code>typedef union MEMBER
  {
      int type;
      struct TYPEA a;
      struct TYPEB b;
  } MEMBER;
  </code></pre>
  
  <p>The union is accessed via:</p>
  
  <pre><code>typedef struct WRAPPER
  {
      union MEMBER* member;
      struct WRAPPER* next;
  } WRAPPER;
  </code></pre>
  
  <p>Questions:</p>
  
  <ol>
  <li>(With 'w' as a pointer to an allocated WRAPPER struct) Accessing using <code>w-&gt;member-&gt;a.id</code> gives "request for member 'id' in something not a structure or union.</li>
  <li>Can I assign a pointer to an already malloc'd TYPEA/B to w->member directly? Or does a union need to be malloced specially?</li>
  </ol>
  
  <p>Thanks.</p>