title: How the while statement is executed in C or how this array-referenced pointers work?
body: <p>I started learning C and I had this exercise from the book &quot;<em>Prentice Hall - The C Programming Language</em>&quot;.</p>
  <p><strong>Chapter 5 Exercise 3:</strong>
  Write a pointer version of the fuction strcat that we showed in Chapter 2. strcat(s, t) copies the string t to the end of s.</p>
  <p>I did the exercise but the first method that came up to my mind was:</p>
  <pre class="lang-c prettyprint-override"><code>void stringcat(char *s, char *t){
      
      int i,j;
      i = j = 0;
      
      while(*(s+i) != '\0'){
          printf(&quot;%d&quot;, i);
          i++;
          
      }
  
      while ( (*(t+j)) != '\0'){
          *(s+i) = *(t+j);
          i++;
          j++;
      }
  }
  </code></pre>
  <p>In main I had:</p>
  <pre class="lang-c prettyprint-override"><code>
  int main(){
      char s[] = &quot;Hola&quot;;
      char t[] = &quot;lala&quot;;
  
      stringcat(s,t);
      printf(&quot;%s\n&quot;, s);
  }
  
  </code></pre>
  <p>At first sight I thought it was right but the actual output was <code>Holalalaa</code>.
  Of course it was not the output that I expected, but then I coded this:</p>
  <pre class="lang-c prettyprint-override"><code>void stringcat(char *s, char *t){
      
      int i,j;
      i = j = 0;
      
      while(*(s+i) != '\0'){
          printf(&quot;%d&quot;, i);
          i++;
          
      }
      
      while((*(s+i) = *(t+j)) != '\0'){
          i++;
          j++;
      }
  }
  </code></pre>
  <p>And the output was right.</p>
  <p>But then I was thinking a lot about the first code because it's very similar to the second one but why the first output was wrong?. Is it something related with the while statement? or something with pointers?. I found it really hard to understand because you can't see what's happening in the array.</p>
  <p>Thanks a lot.</p>