I want to pass an array of arguments to exec() function. The current parameters are all strings. How could I convert the array of string into the array of char array?
Right now I'm using const_cast to remove the const. The code does not look nice. Is there a better solution?
Sample code looks like:
void f(const string &dev, const string &status){
char* args[] = {"link", "set", "dev", const_cast<char*>(dev.c_str()),
const_cast<char*>(status.c_str())};
execv("/sbin/ip", args);
}
char* arg0 = "link"should also be problematic, asconst(or cast here) is missing too...devandstatusmatter here? You're callingexecvand populating it with strings returned bystd::string's that go out of scope aftermainexits.const, and if copy would have be done in function (as suggested in answer, it might avoid an extra copy)).