Ok so this isn't a structs question, but I've moved on to actually getting a command from the user. I want to store the number of arguments and also how many arguments. This is what I have now in function processCommand():
Code:
int processCommand(Funix *funix, char *command)
{
int i, j = 0, argCount = 0;
char *cmd, *str;
char **args;
cmd = strtok(command, " -");
while (str != NULL)
{
str = strtok(NULL, " -");
++argCount;
}
args = (char**)malloc(argCount*sizeof(int));
for(i = 0; i < argCount; i++)
args[i] = (char*)malloc(80*sizeof(char));
cmd = strtok(command, " -");
while (str != NULL)
{
str = strtok(NULL, " -");
strcpy(args[j], str);
j++;
}
return 0;
}
I get a segfault when using this, so obviously something is wrong but I can't find where. This is supposed to work like this: The first time I use strtok is simply to get a count for how many arguments there are. Then I want to dynamically create an array of strings (char **args) that's supposed to hold each argument of the command. So the second time I use strtok I actually want to store each argument into the array. I'm not sure where the error is here, maybe in my dynamically allocating the array of strings (args)?