title: Dynamic variables in C
body: <p>I want to use double set of dynamic variables in my program. I have found similar issue, <a href="https://stackoverflow.com/questions/29195908/is-it-possible-to-use-a-for-loop-to-change-a-variable-name-in-c">Is it possible to use a for loop to change a variable name in C?</a> </p>
  
  <p>I can make similar example from it as </p>
  
  <pre><code>for (str[run] = 0 ; str[run] &lt; 5 ; str[run]++)
  </code></pre>
  
  <p>from the other question, but it will not work in my example.</p>
  
  <p>I have a little bit more advanced version, since I use two dynamic sets of variables at once. I know we can put as many variables as we can in the <em>for</em> loop, using a comma, but even with two it would look really long and not good-looking. So I tried to go in the different way.</p>
  
  <p>What I want is to use <strong>str1</strong> and <strong>chr1</strong> first time.</p>
  
  <p><strong>str2</strong> and <strong>chr2</strong> second time ans so on.</p>
  
  <p>Here is my code:  </p>
  
  <pre><code>int main () {
  
      int run;                            //loop counter
  
      char str1[20] = "abracadabra";      //string #1
      ...
  
      char chr1 = 'a';                    //character #1
      ...
  
      for (run; run &lt;= 5; run++)          // runs 5 times
      {
          printf("The string %c will be removed from %c characters. \n", str[run], chr[run]);
          rmchr(str[run], chr[run]);
          printf("New modified string is: %c \n\n", str[run]);
      }
      ...
  }
  </code></pre>
  
  <p>I know my code is wrong, but I know I am very close to the truth :-) If it would be useful, I also included </p>
  
  <pre><code>#include &lt;string.h&gt; 
  </code></pre>
  
  <p>before my main function.</p>
  
  <p>Here is my full program:</p>
  
  <pre><code>/*
  * A simple program to remove certain characters from the given strings                              
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main () {
  
      int run;                            //loop counter
  
      char str1[20] = "abracadabra";      //string #1
      char str2[20] = "abracadabra";      //string #2
      char str3[20] = "abracadabra";      //string #3
      char str4[20] = "aaaa";             //string #4
      char str5[20] = "aaaa";             //string #5
  
      char chr1 = 'a';                    //character #1
      char chr2 = 'b';                    //character #2
      char chr3 = 'n';                    //character #3
      char chr4 = 'a';                    //character #4
      char chr5 = 'n';                    //character #5
  
      //remove certain characters from array
      void rmchr(char str[], char ch);    //rmchr stands for 'remove character'
  
      for (run; run &lt;= 5; run++)          // runs 5 times
      {
          printf("The string %c will be removed from %c characters. \n", str[run], chr[run]);
          rmchr(str[run], chr[run]);
          printf("New modified string is: %c \n\n", str[run]);
      }
  
      return 0;
  }
  
  void rmchr(char str[], char chr)
  {
     int i, j = 0;                        //loop counters
     int length;                          //length of array
  
     length = strlen(str);
  
     for (i = 0; i &lt; length; i++) {
        if (str[i] != chr) {
           chr = str[i];
           str[j] = chr;
           j++;
        }
     }
     str[j] = '\0';
  }
  </code></pre>
  
  <p>FINAL version of a program, it runs perfectly and got approved by my teacher:</p>
  
  <pre><code>/*
  * A simple program to remove certain characters from the given strings                              
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main () {
  
      // print array before and after editing array
      void print_rmchr (char str[], char ch); 
  
      int run;                            //loop counter
  
      char str1[20] = "abracadabra";      //string #1
      char str2[20] = "abracadabra";      //string #2
      char str3[20] = "abracadabra";      //string #3
      char str4[20] = "aaaa";             //string #4
      char str5[20] = "aaaa";             //string #5
  
      char chr1 = 'a';                    //character #1
      char chr2 = 'b';                    //character #2
      char chr3 = 'n';                    //character #3
      char chr4 = 'a';                    //character #4
      char chr5 = 'n';                    //character #5
  
      print_rmchr(str1, chr1);
      print_rmchr(str2, chr2);
      print_rmchr(str3, chr3);
      print_rmchr(str4, chr4);
      print_rmchr(str5, chr5);
  
      return 0;
  }
  
  //remove certain characters from array
  void rmchr(char str[], char ch) {
     int i, j = 0;    //loop variable
     int size;        //lengh 
     char new_str[20];    //new array
  
     size = strlen(str);
  
     for (i = 0; i &lt; size; i++) {
        if (str[i] != ch) {
           new_str[j] = str[i];
           j++;
        }
     }
     new_str[j] = '\0';
  
     strcpy(str, new_str);
  
  }
  
  // print array before and after editing array
  void print_rmchr (char str[], char ch){
  
      //remove certain characters from array
      void rmchr(char str[], char ch);
  
      printf("The string '%s' will be removed from '%c' characters. \n\n", str, ch);
  
      rmchr(str, ch);
  
      printf("New modified string is: '%s'. \n\n", str);
  }
  
      /* In case you will need user input:
      //USER INPUT
      printf("Enter the string : \n");
      gets(str);
  
      printf("Enter character which you want to delete : \n");
      scanf("%ch", &amp;ch);
  
      print_rmchr(str, ch);
      */
  </code></pre>