title: Allocating memory for a array to char pointer
body: <p>The following piece of code gives a segmentation fault when allocating memory for the last arg. What am I doing wrong? Thanks.</p>
  
  <pre><code>    int n_args = 0, i = 0;
      while (line[i] != '\0')
      {
          if (isspace(line[i++]))
              n_args++;
      }
  
      for (i = 0; i &lt; n_args; i++)
          command = malloc (n_args * sizeof(char*));
  
      char* arg = NULL;
      arg = strtok(line, " \n");
      while (arg != NULL)
      {
          arg = strtok(NULL, " \n");
              command[i] = malloc ( (strlen(arg)+1) * sizeof(char) );
          strcpy(command[i], arg);
          i++;
      }
  </code></pre>
  
  <p>Thanks.</p>