The following piece of code gives a segmentation fault when allocating memory for the last arg. What am I doing wrong? Thanks.
int n_args = 0, i = 0;
while (line[i] != '\0')
{
if (isspace(line[i++]))
n_args++;
}
for (i = 0; i < 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++;
}
Thanks.