So I saw this example:
int main()
{
char *ls[] = {"ls", NULL};
char *grep[] = {"grep", "pipe", NULL};
char *wc[] = {"wc", NULL};
char **cmd[] = {ls, grep, wc, NULL};
loop_pipe(cmd);
return (0);
}
And loop_pipe() is a function to manipulate the arguments for multiple pipes. But the variables on this code which are inserted into cmd are already defined. On my personal code I'm trying to do the opposite, create the variable, insert the arguments into the variable and then add it to cmd. But what isn't happening is the 'adding' need. For instance, the code look like this:
*(cmd[pipe_count]) = arg;
I guess there is something wrong ('cause it's not working) but isn't using an index to set the location of insertion sufficient to allocate a pointer of array inside my double pointer? If not can someone help me clear how to assign like this?
Edit1:
My code simplified is like this:
char* arg[100];
char** cmd[100] = {};
int pipe_count = 0;
int index = 0;
arg[pipe_count] = (char *) malloc(sizeof(char) * 100);
for (int k = 0; params_vector[k][0] != '\0'; k++) { // percorrer todos os parametros. exemplo: ls | less | sort
if (strcmp(params_vector[k], "|") != 0) {
arg[index] = params_vector[k];
index++;
} else {
arg[index] = '\0';
*(cmd) = arg;
pipe_count++;
arg[pipe_count] = (char *) malloc(sizeof(char) * TAM);
index = 0;
}
}