title: Double pointers - output explanation
body: <pre><code>#include &lt;stdio.h&gt;
  int main(void){
  
      int a=1,b=2,c=3; int *p,*q,**r; p=&amp;a;
      r=&amp;q;
      q=&amp;c;
      a=*q+**r;
      printf("x=%d y=%d z=%d\n",**r,*p,*q); 
      *r=p;
      a=*q+**r;
      printf("w=%d\n",a);
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>x=3 y=6 z=3
  w=12
  </code></pre>
  
  <p>I was able to predict the output correctly, but I am not sure whether I have the correct explanation for the output of z.</p>
  
  <p>Please see whether I have the correct understanding:</p>
  
  <ul>
  <li>Right before <code>*r=p;</code> executes, we have <code>a=6,b=2,c=3</code>.</li>
  <li>When  <code>*r=p;</code> executes, the value at the place to which <code>r</code> points to gets changed to <code>p</code>.</li>
  <li>Now <code>r</code> points to <code>q</code> that has address of <code>c</code>, so now <code>q</code> has address of <code>a</code> because <code>p</code> points to <code>a</code>. So <code>q</code> now points to <code>a</code>. So <code>*q</code> gives 6.</li>
  <li>Since <code>r</code> still points to <code>q</code>, and <code>q</code> points to <code>a</code>, <code>**r</code> gives 6.</li>
  <li>So <code>*q + **r = 6+6=12</code></li>
  </ul>
  
  <p>Is this the correct explanation?</p>