title: C struct, union pointer to struct
body: <p>I have a typedeffed struct and an union. The union contains the struct and a single <code>uint32_t</code>. The goal is to assign a value to <code>foo</code> that corresponds to the 'bits' in the struct.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  
  typedef struct {
      uint32_t valA :1;
      uint32_t valB :1;
      uint32_t valC :1;
      uint32_t valD :1;
      uint32_t valE :1;
  } ValStruct_Type;
  
  typedef union {
      ValStruct_Type valStruct;
      uint32_t valUint;
  } ValUnion_Type;
  
  uint32_t foo = 0;
  
  
  int main(void)
  {
      ValStruct_Type Vals;
      Vals.valA = 0x0;
      Vals.valB = 0x1;
      Vals.valC = 0x0;
      Vals.valD = 0x1;
      Vals.valE = 0x1;
  
      ValStruct_Type *Vals_ptr;
      Vals_ptr = &amp;Vals;
  
      foo = ((ValUnion_Type *)Vals_ptr)-&gt;valUint;
  
      return 0;
  }
  </code></pre>
  
  <p>foo becomes:</p>
  
  <pre><code>Decimal:    4194330
  Hex:        0x40001a
  Binary:     10000000000000000011010
  </code></pre>
  
  <p>Who can explain what is happening here exactly (union pointer to a struct pointer, defererenced to a union member?)?</p>
  
  <p>Secondly: why is bit 22 of <code>foo</code> set in addition to bit 1,3 and 4?</p>