Originally Posted by
bluedalmatian
what would I declare the array as then in order than I can copy a series of variable length strings from the vector into it.
This works for me:
Code:
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>
#include <cstring>
int main()
{
std::vector<std::string> args;
args.push_back("ls");
args.push_back("-l");
const size_t array_size = args.size();
char** arg_array = new char*[array_size + 1]; // extra space for terminating NULL
for (size_t i = 0; i < array_size; ++i)
{
arg_array[i] = strdup(args[i].c_str());
}
arg_array[array_size] = 0;
execvp(arg_array[0], arg_array);
std::cout << "This statement is never reached." << std::endl;
}
Bookmarks