Your cmdArgs1
is a pointer to a pointer, meaning that it is not enough to allocate space for the array itself. You need to allocate space for the individual arrays of characters (or for the individual C string).
You can do it in a separate call of malloc
for(counter = 0; counter < argsCount; counter++)
{
cmdArgs1[counter] = malloc(strlen("ls")+1); // +1 for null terminator
strcpy(cmdArgs1[counter], "ls");
}
or with strdup
:
for(counter = 0; counter < argsCount; counter++)
{
cmdArgs1[counter] = strdup("ls");
}
In both cases your program is liable for free
ing the elements of the array before free
ing the array itself:
for(counter = 0; counter < argsCount; counter++)
{
free(cmdArgs1[counter]);
}
free(cmdArgs1);