title: Assigning address of pointer to pointer-to-pointer
body: <p>I am trying to make a simple piece of c code using pointers work but memory is being overwritten unexpectedly.</p>
  
  <p>I want to create an array of pointers to integers then create a pointer to an integer and assign it's address to an array.</p>
  
  <p>Therefore, the array will point to a pointer to an integer. I did this in a function called <code>add_value_to_array()</code>.</p>
  
  <p>Here is my code :</p>
  
  <pre class="lang-c prettyprint-override"><code>void add_value_to_array(int *** array, int * value) {
      *array = &amp;value;
  }
  int main() {
  
      int ** arr = { 0 };
      int value = 5;
      int * value_ptr =  &amp;value;
  
      add_value_to_array(&amp;arr, value_ptr);
      //arr = &amp;value_ptr;
      printf("looool\n");
      printf("looool\n");
      printf("looool\n");
      printf("looool\n");
      printf("%p\n", arr[0]);
  
  }
  </code></pre>
  
  <p>What I want is :</p>
  
  <pre><code>arr -&gt; value_ptr -&gt; 5
  (arr = &amp;value_ptr
  *array = value_ptr
  value_ptr = &amp;value
  *value_ptr = 5
  **arr = 5)
  </code></pre>
  
  <p>however, this is what I have right after <code>add_value_to_array()</code> is called, but memory is overwritten when I call the <code>printfs()</code>, I get garbage values in my <code>arr</code> variable.</p>
  
  <p>Even weirder, if I do directly <code>arr = &amp;value_ptr</code> instead of calling <code>add_value_to_array</code>, things go as expected and memory is not overwritten by the <code>printfs()</code>.</p>
  
  <p>So it seems that memory is allocated differently if I use a function or if I do I do things outside of it.</p>
  
  <p>What is happening that I am seeing this behavior?</p>