what's the prob with this code?

145 Views Asked by At

Am trying to call evecvp() with these arguments:

vector<string>subcommand;
void parse(char *str)
{
    pid_t pid;
    char *cmd1=(char *)malloc(sizeof(300));

    cmd1=strtok(str," ");
    while(cmd1!=NULL)
    {
        subcommand.push_back(cmd1);
        cmd1=strtok(NULL," ");
    }

    subcommand.push_back('\0');
    vector<char const*> v( subcommand.size() );
    for( int i = 0; i < v.size(); ++i )
    {
        v[i] = subcommand[i].c_str();
    }

    fork();
    if(pid==0)
        execvp(subcommand[0].c_str(),v);

}

I get this error:

main1.cpp: In function ‘void parse(char*)’:
main1.cpp:80:34: error: cannot convert ‘std::vector<const char*>’ to ‘char* const*’ for argument ‘2’ to ‘int execvp(const char*, char* const*)’

Tried all permutations with typecasting but just can't get it to work. What's the problem?

1

There are 1 best solutions below

1
On

The variable v is vector<char const*> in the statement execvp(subcommand[0].c_str(),v);, while the declaration of evecvp is int execvp(const char*, char* const*).