title: casting void* to struct * in C++
body: <p>I have the following code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  typedef unsigned char byte;
  
  typedef  struct {
      unsigned int a;
      unsigned int b;
  } Struct_A;
  
  void* malloc_(size_t s, byte* heap, int *next) {
      int old = *next;
      *next = *next + s;
      return heap + old;
  }
  
  void f(Struct_A *sa, byte* heap, int *next) {
      sa = (Struct_A*) malloc_(8, heap, next);
      sa-&gt;a = 10;
      sa-&gt;b = 20;
  }
  
  int main() {
      byte *heap = new byte[1000];
      int next;
      next = 0;
  
      Struct_A *sa;
      sa = (Struct_A*) malloc_(8, heap, &amp;next);
      sa-&gt;a = 100;
      sa-&gt;b = 200;
  
      cout &lt;&lt; sa-&gt;a &lt;&lt; endl;
  
  
      Struct_A *sb;
      f(sb, heap, &amp;next);
      cout&lt;&lt; sb-&gt;a &lt;&lt;endl;
  
  
      return 0;
  
  }
  </code></pre>
  
  <p>the code works well for sa but not for sb!!!
  function f() does exactly the same thing the three code lines after "Struct_A *sa;" does. Any idea what is wrong with function f()?</p>