title: Copying Command Line Arguments Into an Array
body: <p>For a program, I would like to make an array copy of the arguments sent in by command line using malloc().</p>
  
  <p>So for example if I do ./a.out one two three
  I want an array with {a.out, one, two, three} in it. </p>
  
  <p>However, I have some issues getting my program to work. Here's what I have:</p>
  
  <pre><code>static char** duplicateArgv(int argc, char **argv)                                                                                                                                                         
  {                                                                                                                                                                                                                                                                                                                                                                                       
      char *array;                                                                                                                                                                                                                                                                                                                                                               
      int j = 0;                                                                                                                                                                                        
  
      // First allocate overall array with each element of char*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
      array = malloc(sizeof(char*) * argc);                                                                                                                                                       
      int i;                  
  
      // For each element allocate the amount of space for the number of chars in each      argument                                                                                                                                                              
      for(i = 1; i &lt; (argc + 1); i++){                                                                                                                                                                      
          array[i] = malloc(strlen(*(argv + i)) * sizeof(char));                                                                                                                                        
          int j;       
  
          // Cycle through all the chars and copy them in one by one                                                                                                                                                                                 
          for(j = 0; j &lt; strlen(*(argv + i)); j++){                                                                                                                                                     
              array[i][j] = *(argv + i)[j];                                                                                                                                                                                                                                                                                                                                                       
          }                                                                                                                                                                                             
  
      }                                                                                                                                                                                                 
  
      return array;
  
  }
  </code></pre>
  
  <p>As you might imagine, this doesn't work. I apologize ahead of time if this somehow totally doesn't make sense, as I just started learning about pointers. Also, I'm not quite sure how to write code to free up every element in the *array after I do what I need to the copy. </p>
  
  <p>Could anyone give me some tips on what I should look into to make it do what I want?</p>
  
  <p>Thanks for any help! </p>