title: Infinite Loop when using 'for' with pointer as loop variable
body: <p>I am quite new to coding and got stuck with a problem. I tried to solve it myself and have been googling a lot, but I still do not have a solution to this. Maybe one of you can help? </p>
  
  <p>This is my code:</p>
  
  <pre><code>int main(int argc, char **argv) {
      struct node {
          char *str;
          int count;
          struct node *next;
      };
  
      struct node head = { argv[1], 1, NULL };
      for (int i = 2; i &lt; (argc); i++) {
          for (struct node *p = &amp;head; (p != NULL); p = p-&gt;next) {
              printf("%s,%s\n", argv[i], p-&gt;str);
              if (strcmp(argv[i], p-&gt;str) == 0) {
                  printf("case1\n");
                  p-&gt;count++;
                  break;
              }
              else if ((strcmp(argv[i], p-&gt;str) != 0) &amp;&amp; p-&gt;next) {
                  printf("case2\n");
                  printf("Adresse, auf die p zeigt: %p", &amp;p);
                  continue;
              }
              else if ((strcmp(argv[i], p-&gt;str) != 0) &amp;&amp; (!p-&gt;next)) {
                  printf("case3\n");
                  struct node *oldhead = &amp;head;
                  head.str = argv[i];
                  head.count = 1;
                  head.next = oldhead;
                  break;
              }
  
          }
      }
  
      // Print how many times each string appears
  
      return 0;
  }
  </code></pre>
  
  <p>The goal is to create a linked list that contains all the arguments I gave to the <code>main()</code> when calling the program. If there is a duplicate, the structure should count them. For example, if i call the program like <code>./a.out foo fool foo</code> the result should be a list of length two, where the first element contains the string <code>"foo"</code> and count <code>2</code>, and the second element contains the string <code>"fool"</code> and has a count of <code>1</code>. The problem is the <code>else if</code>-statement within the inner for loop. This is the only part where the inner for loop should actually be used and assign  <code>p-&gt;next</code> to <code>p</code>. Unfortunately that is not happening. The result is that the inner for loop starts over and over again and the pointer p points to the same address all the time (I used printf to figure that out). </p>
  
  <p>Does any of you have an idea what could be the problem here? I tried everything I could and tried to find a solution online ...</p>
  
  <p>Thanks a lot!!!</p>