title: How to pass a union as a parameter to a function
body: <p>This code is for a driver for a <a href="http://en.wikipedia.org/wiki/Digital-to-analog_converter" rel="noreferrer">DAC</a> chip.</p>
  
  <p>I have a bitfield below which represents a 24-bit register.  So what I need to do, is populate the bitfield and write it out over <a href="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus" rel="noreferrer">SPI</a> to the chip.</p>
  
  <pre><code>    typedef struct {
          uint8_t rdwr_u8:       1;
          uint8_t not_used_u8:   3;
          uint8_t address_u8:    4;
          uint8_t reserved_u8:   8;
          uint8_t data_u8:       8;
          uint8_t padding_u8:    8;
      } GAIN_REG_st;
  </code></pre>
  
  <p>In my initialisation function I create a union as below.</p>
  
  <pre><code>    union{
          GAIN_REG_st GAIN_st;
          uint32_t G_32;
      } G_u;
  </code></pre>
  
  <p>Now I need to pass a GAIN_REG_st bitfield to a function which will populate it.</p>
  
  <p>Once it's populated I can assign the bitfield to a 32-bit integer and pass that integer to a low level function to write over SPI.</p>
  
  <p>How do I pass the bitfield GAIN_REG_st to a function when it's inside a union? (Can you show a function prototype and call)?</p>
  
  <p>How does the function access the bitfield's members? (would it be like <code>G_u.GAIN_st.rdwr_u8 = 1</code>?)</p>