title: C Pointer Assignment in a function
body: <p>Why can't I assign a point in a function. As you noticed in the following code. I can not assign pointer p1 pointed to the correct address after the function return. But with the global pointer *p, I can store the address information. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int *p = NULL;
  void test(int * pt1, int**pt2){
      p = (int*)malloc(sizeof(int));    
      pt1 = p;
      *pt2 = p;
      printf("p points to %p\n", p);
      printf("pt1 points to %p\n", pt1);
      printf("pt2 points to %p\n", *pt2);
  }
  
  int main(void) {
      int *p1 = NULL; 
      int *p2 = NULL;
  
      printf("p points to %p\n", p);
      printf("p1 points to %p\n", p1);
      printf("p2 points to %p\n", p2);
  
      test(p1, &amp;p2);
  
      printf("p points to %p\n", p);
      printf("p1 points to %p\n", p1);
      printf("p2 points to %p\n", p2);
  
      return 0;
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>p points to (nil)
  p1 points to (nil)
  p2 points to (nil)
  p points to 0x8acb008
  pt1 points to 0x8acb008
  pt2 points to 0x8acb008
  p points to 0x8acb008
  p1 points to (nil)
  p2 points to 0x8acb008
  </code></pre>