title: Cannot write integer to an offset within a buffer (char*)
body: <p>I'm trying to write an <code>unsigned integer</code> to a specific offset in buffer in my C program. The buffer is typical 1 byte <code>char *</code> buffer.
  I'm using <code>memcpy</code> to do this with some pointer arithmetic to point <code>memcpy</code> destination to a particular offset withing that buffer.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>char* ph = (char*) malloc(4096);
  
  //Init buffer with '\0'
  memset(ph, '\0', 4096);
  
  //Set int to be written
  unsigned int tupleCnt = 4;
  
  //Write to 4th byte offset (int* + 1)
  memcpy(((int*) ph) + 1, (void *) &amp;tupleCnt, sizeof(tupleCnt));
  </code></pre>
  
  <p>However, this doesn't write anything to this buffer.</p>
  
  <p>Here's the hexdump of the file to which this buffer is written:</p>
  
  <pre><code>0000000 0000 0000 0000 0000 0000 0000 0000 0000
                 ^
  </code></pre>
  
  <hr>
  
  <p><strong>If I write it to 0th offset, it works:</strong></p>
  
  <pre><code>//Write to 0th byte offset (int* + 0)
  memcpy(((int*) ph) + 0, (void *) &amp;tupleCnt, sizeof(tupleCnt));
  </code></pre>
  
  <p>Here's the hexdump:</p>
  
  <pre><code>0000000 0004 0000 0000 0000 0000 0000 0000 0000
            ^
  </code></pre>
  
  <hr>
  
  <p>By the way I'm using <code>fwrite</code> to write this buffer to file, if it makes any difference.</p>
  
  <pre><code>fwrite(ph, 1, strlen(ph), fp);
  </code></pre>
  
  <hr>
  
  <p>I also tried using byte by byte increment on char* pointers, it didn't help either.
  For example:</p>
  
  <pre><code>//Write to 4th byte offset (int* + 1)
  memcpy(ph + 4, (void *) &amp;tupleCnt, sizeof(tupleCnt));
  </code></pre>
  
  <hr>
  
  <p>Thanks in advance!
  Or is there any other way to write int (or any numeric) values to char* buffers? Except int to string conversation, which I really want to avoid. I think it's too much overhead and naive method. :)</p>