I have an array of strings containing the input parameters for execvp
. How can I turn it into an array of string pointers for execvp
?
For command with one argument, two strings are present:
char param[4][10] = ["wc","file.txt"]
And with two arguments:
char param[4][10] = ["cp","file1.txt","file2.txt"]
If I know the number of arguments in advance, I can simply write
char *arg[]={param[0],param[1],NULL}
execvp(arg[0],arg);
or
char *arg[]={param[0],param[1], param[2], NULL}
execvp(arg[0],arg);
respectively.
But what can I do when I don’t know the number of arguments?
I tried looping
int count =4;
char* arg[count];
for(int i=0;i<count;i++)
{
strcpy(exe[i],param[i]);
printf("%s\n",exe[i]);
}
strcpy(exe[count],'\0');
but that gave me segfaults.