title: How to set pointer reference through a function
body: <p>In C, I am trying to set a pointer's value by sending it to a function, but the value wont change outside of the function. Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  void foo(char* str) {
  
      char* new_str = malloc(100);
      memset(new_str, 0, 100);
      strcpy(new_str, (char*)"new test");
  
      str = new_str;
  }
  
  
  int main (int argc, char *argv[]) {
  
      char* str = malloc(100);
      memset(str, 0, 100);
  
      strcpy(str, (char*)"test");
  
      foo(str);
  
      printf("str = %s\n", str);
  }  
  </code></pre>
  
  <p>I want to print out:</p>
  
  <pre><code>str = new test 
  </code></pre>
  
  <p>but this code prints out: </p>
  
  <pre><code>str = test
  </code></pre>
  
  <p>Any help will be appreciated. Thanks in advance.</p>