Thinking in C++ Exercise
<p>I'm learning C++ and I'm following the "Thinking in C++" book, I'm doing the exercises and I can't understand why in this exercise:</p>

<blockquote>
  <p>Define an array of <code>int</code>. Take the starting address of that array and use <code>static_cast</code> to convert it into an <code>void*</code>. Write a function that takes a <code>void*</code>, a number (indicating a number of bytes), and a value (indicating the value to which each byte should be set) as arguments. The function should set each byte in the specified range to the specified value. Try out the function on your array of <code>int</code>.</p>
</blockquote>

<p>this solution works</p>

<pre><code>#include&lt;iostream&gt;

using namespace std;

#define PR(EX) cout &lt;&lt; #EX &lt;&lt; ": " &lt;&lt; EX &lt;&lt; endl;

void f1( void* v, size_t num_bytes, int val){

  unsigned char* p_addr = reinterpret_cast&lt;unsigned char*&gt;(v);
  unsigned char byte = static_cast&lt;unsigned char&gt;(val);

  for( size_t i = 0; i &lt; num_bytes; i++){
    *p_addr = byte++;
    p_addr++;
  }

  PR(num_bytes);

}

int main(){

  int a[5] = { 0, 0, 0, 0, 0 };
  int value = 67;
  void* vp = static_cast&lt;void*&gt;(&amp;a);    

  f1(vp, sizeof(a), value);

  unsigned char* byte = reinterpret_cast&lt;unsigned char*&gt;(vp);


  for( int i = 0; i &lt; sizeof(a) / sizeof(a[0]); i++){
    cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = ";
    for( int j = 0; j &lt; sizeof(int); j++){
      cout &lt;&lt; reinterpret_cast&lt;int*&gt;(*byte) &lt;&lt; " ";
      byte++;
    }
    cout &lt;&lt; endl;
  }

  return 0;

}
</code></pre>

<p>while this one does not</p>

<pre><code>#include&lt;iostream&gt;

using namespace std;

#define PR(EX) cout &lt;&lt; #EX &lt;&lt; ": " &lt;&lt; EX &lt;&lt; endl;

void f1( void* v, size_t num_bytes, int val){

  unsigned char* p_addr = reinterpret_cast&lt;unsigned char*&gt;(v);
  unsigned char byte = static_cast&lt;unsigned char&gt;(val);

  for( size_t i = 0; i &lt; num_bytes; i++){
    *p_addr = byte++;
    p_addr++;
  }

  PR(num_bytes);

}

int main(){

  int a[5] = { 0, 0, 0, 0, 0 };
  int value = 67;
  void* vp = static_cast&lt;void*&gt;(&amp;a);
  unsigned char* byte = reinterpret_cast&lt;unsigned char*&gt;(vp);

  for( size_t i = 0; i &lt; (sizeof(a) / sizeof(a[0])); i++){
    cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = ";
    for( size_t j = 0; j &lt; sizeof(int); j++){
       cout &lt;&lt; *byte;
       byte++;
    }
    cout &lt;&lt; endl;
  }

  PR(*byte);


  f1(vp, sizeof(a), value);    

  for( int i = 0; i &lt; sizeof(a) / sizeof(a[0]); i++){
    cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = ";
    for( int j = 0; j &lt; sizeof(int); j++){
      cout &lt;&lt; reinterpret_cast&lt;int*&gt;(*byte) &lt;&lt; " ";
      byte++;
    }
    cout &lt;&lt; endl;
  }

  return 0;

}
</code></pre>

<p>The differences are in where i define the pointer <code>byte</code>, in the "working" version it is before the function call while in the "not working" version it is before. The <code>cout</code> of the two versions are</p>

<pre><code>a[0] = 
a[1] = 
a[2] = 
a[3] = 
a[4] = 
*byte: 
num_bytes: 20
a[0] = 0 0 0 0 
a[1] = 0x3b 0x8 0x40 0 
a[2] = 0 0 0 0 
a[3] = 0x4 0 0 0 
a[4] = 0 0 0 0
</code></pre>

<p>and</p>

<pre><code>num_bytes: 20
a[0] = 0x43 0x44 0x45 0x46 
a[1] = 0x47 0x48 0x49 0x4a 
a[2] = 0x4b 0x4c 0x4d 0x4e 
a[3] = 0x4f 0x50 0x51 0x52 
a[4] = 0x53 0x54 0x55 0x56 
</code></pre>

<p>in the second one i thing that byte are changing in the right way, while in the first one it seems to me that something is going wrong. Apparently the issue is on where i define the <code>byte</code> pointer and, since I'm a beginner and learning by myself, I wish to understand what I'm thinking wrong.</p>

<p>Ok, I've understood my error thanks to you suggestions, I've modified the code and now it prints what I was expecting</p>

<pre><code>#include&lt;iostream&gt;

using namespace std;

#define PR(EX) cout &lt;&lt; #EX &lt;&lt; ": " &lt;&lt; EX &lt;&lt; endl;

void f1( void* v, size_t num_bytes, int val){

  unsigned char* p_addr = reinterpret_cast&lt;unsigned char*&gt;(v);
  unsigned char byte = static_cast&lt;unsigned char&gt;(val);

  for( size_t i = 0; i &lt; num_bytes; i++){
    *p_addr = byte++;
    p_addr++;
  }

  PR(num_bytes);

}

int main(){

  int a[5] = { 0, 0, 0, 0, 0 };
  int value = 67;
  void* vp = static_cast&lt;void*&gt;(a);
  unsigned char* byte = reinterpret_cast&lt;unsigned char*&gt;(vp);

  for( size_t i = 0; i &lt; (sizeof(a) / sizeof(a[0])); i++){
    cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = ";
    for( size_t j = 0; j &lt; sizeof(int); j++){
      cout &lt;&lt; reinterpret_cast&lt;int*&gt;(*byte);
      byte++;
    }
    cout &lt;&lt; endl;
  }

  byte = reinterpret_cast&lt;unsigned char*&gt;(vp);

  f1(vp, sizeof(a), value);

  for( int i = 0; i &lt; sizeof(a) / sizeof(a[0]); i++){
    cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = ";
    for( int j = 0; j &lt; sizeof(int); j++){
      cout &lt;&lt; *byte &lt;&lt; " ";
      byte++;
    }
    cout &lt;&lt; endl;
  }

  return 0;

}

a[0] = 0000
a[1] = 0000
a[2] = 0000
a[3] = 0000
a[4] = 0000
num_bytes: 20
a[0] = C D E F 
a[1] = G H I J 
a[2] = K L M N 
a[3] = O P Q R 
a[4] = S T U V 
</code></pre>
