title: C Function to Convert float to byte array
body: <p>I'm trying to make a function that will accept a float variable and convert it into a byte array. I found a snippet of code that works, but would like to reuse it in a function if possible. </p>
  
  <p>I'm also working with the Arduino environment, but I understand that it accepts most C language.</p>
  
  <p><b>Currently works:</b></p>
  
  <pre><code>float_variable = 1.11;
  byte bytes_array[4];
  
  *((float *)bytes_array) = float_variable;
  </code></pre>
  
  <p><b>What can I change here to make this function work?</b></p>
  
  <pre><code>float float_test = 1.11;
  byte bytes[4];
  
  // Calling the function
  float2Bytes(&amp;bytes,float_test);
  
  // Function
  void float2Bytes(byte* bytes_temp[4],float float_variable){ 
       *(float*)bytes_temp = float_variable;  
  }
  </code></pre>
  
  <p>I'm not so familiar with pointers and such, but I read that <em>(float</em>) is using casting or something?</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Cheers</p>
  
  <p><i>*EDIT: SOLVED</i></p>
  
  <p>Here's my final function that works in Arduino for anyone who finds this. There are more efficient solutions in the answers below, however I think this is okay to understand.</p>
  
  <p><b>Function: converts input float variable to byte array</b></p>
  
  <pre><code>void float2Bytes(float val,byte* bytes_array){
    // Create union of shared memory space
    union {
      float float_variable;
      byte temp_array[4];
    } u;
    // Overite bytes of union with float variable
    u.float_variable = val;
    // Assign bytes to input array
    memcpy(bytes_array, u.temp_array, 4);
  }
  </code></pre>
  
  <p><b>Calling the function </b></p>
  
  <pre><code>float float_example = 1.11;
  byte bytes[4];
  
  float2Bytes(float_example,&amp;bytes[0]);
  </code></pre>
  
  <p>Thanks for everyone's help, I've learnt so much about pointers and referencing in the past 20 minutes, Cheers Stack Overflow!</p>