title: Unsigned char array concatenation in C
body: <p>What's the best way to concatenate unsigned char arrays in C? Furthermore, is there a way to concatenate unsigned char arrays with char arrays? 2 of these unsigned char arrays are really just strings, but for simplicity, I'm treating them as unsigned char arrays.</p>
  
  <p>The requirement is complex: there is a function that will take 1 (one) unsigned char array.  That one unsigned char array is really 4 variables concatenated to make up that 1 unsigned char array. To add to the complexity, the first unsigned char array is really just a string of variable length, but its max length is 60 (i.e. sometimes it would have length = 15, other times = 60).</p>
  
  <pre><code>someFunctionAssignsFirst(unsigned char *first) 
  {
     //it could be 15 or 60 chars long.
     ...
  }
  
  unsigned char first[60] = //someFunctionAssignsFirst() //This is a string i.e. "variable size string max size 60"
  unsigned char second[8] = "always8."; //This is a string i.e. "01234567"
  unsigned char third[32] = "always32"; //This is a cryptographic key
  unsigned char fourth[32] = "always32"; //This is a cryptographic key
  </code></pre>
  
  <p>How would I go about getting:</p>
  
  <pre><code>unsigned char allstrings[sizeof(first)+sizeof(second)+sizeof(third)+sizeof(fourth)] = //all strings combined
  </code></pre>
  
  <p>?</p>
  
  <p>I attempted some for loops, but the variable length <code>first</code> is disrupting the concatenation, and I'm sure there has to be a better way.</p>
  
  <p>Full Disclosure: I'm not an expert, and I don't necessarily love C. Also for the requirement, not allowed C++ or any other language.</p>
  
  <hr>
  
  <p>This is what I was trying to do, and (for clarification) I don't get a null character at the end so it's not really a string.</p>
  
  <pre><code>unsigned char *first = "this is a sample string, human readable";
  unsigned char *second = "12345678" //always a number
  //unsigned char third -&gt; I have the value from before and it's a key
  //unsigned char fourth -&gt; I have the value from before and it's a key
  unsigned char allstrings[sizeof(first) + sizeof(second) + sizeof(third) + sizeof(fourth)];
      int counter = 0;
      for (int i = 0; i &lt;= sizeof(first); i++)    
      {
          allstrings[counter] = first[i];
          counter++;
      }
      for (int i = 0; i &lt;= sizeof(second); i++)       
      {
          allstrings[counter] = second[i];
          counter++;
      }
      for (int i = 0; i &lt;= sizeof(third); i++)
      {
          allstrings[counter] = third[i];
          counter++;
      }
      for (int i = 0; i &lt;= sizeof(fourth); i++)
      {
          allstrings[counter] = fourth[i];
          counter++;
      }
  </code></pre>
  
  <p>The <code>allstrings</code> variable, doesn't get anything beyond "readable" in my example above.</p>