title: C Arrays Not Retaining Values
body: <p>I'm writing a simple Base64 decoding function in C. The output of this function looks correct (based on comparing the result with existing tools) when using the printf() debugging lines as below:</p>
  
  <pre><code>unsigned char * b64decode(char *line)
  {
      char *idx;
      unsigned char *decode;
      int i,j,len,dlen,hold = 0;
  
      idx  = strchr(line, '\n');        //index of newline
      len  = idx - line;                //lenght of string
      dlen = 3 * (len / 4);             //decoded length
  
      decode = malloc((sizeof(unsigned char) * dlen) + 1);
      pad = 0;
  
      for (i = 0; i &lt;= len; i++) {      //deindex from ASCII
          line[i] = deindex(line[i]);
      }
  
      for(i = 0, j = u0; i &lt; len; i++,j++) { 
          hold |= (line[i] &lt;&lt; 18);  
          hold |= (line[++i] &lt;&lt; 12);
          hold |= (line[++i] &lt;&lt; 6);
          hold |= line[++i];
  
          decode[j] = hold &gt;&gt; 16;
          printf("%d ", decode[j]);
          decode[++j] = (hold &gt;&gt; 8) &amp; 0xFF;
          printf("%d ", decode[j]);
          decode[++j] = hold &amp; 0xFF;
          printf("%d ", decode[j]);
  
      hold = 0;
      }
  
      if (pad)                          //terminate before padding
          decode[dlen - pad] = '\0';
  
      return decode;
  
  }
  </code></pre>
  
  <p>Which produces the (partial) output:</p>
  
  <pre><code>29 66 31 77 11 15 2 31 79 19 78 60 26 105 101 31 73 28 14 78 19 1 11 7 
  78 27 1 22 69 54 0 30 1 73 100 32 84 29 29 67 51 83 78 101 82 6 0 71 84
  28 13 69 77 7 4 12 83 18 60 12 30 8 73 26 9 17 79 20 76 33 26 71 43 0 5
  29 71 89 17 4 9 0 100 38 7 83 0 55 22 6 12 26 23 65 29 1 82 84 48 95 0 32
  19 10 5 71 79 18 72 8 69 78 101
  </code></pre>
  
  <p>The problem arises when I try to access this array after exiting the for loop with those printf() lines. This for loop (maximum i value is arbitrary) ...</p>
  
  <pre><code>for (i = 0; i &lt; 100; i++) {
      printf("%d ", decode[i]);
  }
  </code></pre>
  
  <p>Produces this output...</p>
  
  <pre><code>29 66 31 77 11 15 2 31 79 19 78 60 26 105 101 31 73 28 14 78 19 1 11 7 78
  27 1 22 69 54 0 30 1 73 100 32 84 29 29 67 51 83 78 101 82 6 0 71 84 28 
  13 69 77 7 4 12 83 18 60 12 30 8 73 26 9 17 79 20 76 33 26 71 43 0 5 29
  71 89 17 4 9 0 100 38 7 83 0 55 22 6 12 26 23 65 29 1 82 84 48 95 0 32 19
  10 5 71 79 168 187 158 23 131 127 0 0 168 187 158 23 131 127 0 0 0 0 0 0
  0 0 0 0 65 0 0 0 0 0 0 0 88 187 158 23 131 127 0 0 88 187 158 23 131 127
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0
  0 0 0 0 0 0 16 16 0 0 0 0 0 0 72 85 73 102
  </code></pre>
  
  <p>Can anyone let me know what I'm doing wrong?</p>