Вероятно, лучше объявить args
как массив char const*
:
char const* args[] = {"link", "set", "dev", dev.c_str(), status.c_str()};
Однако, если вам действительно нужен char*
, а не char const*
, как требует execv
, нам нужно std::string::data
:
void f(const string &dev_, const string &status_){
// Note: string literals don't convert to char* in C++,
// although some compilers allow it as an extension.
// Declare these as arrays so we can form `char*`s to them:
char link[] = "link";
char set[] = "set";
char dev_lit[] = "dev";
// No choice but to copy the const args:
std::string dev{dev_};
std::string status{status_};
// C++17-specific:
char* args[] = {link, set, dev_lit, dev.data(), status.data()};
execv("/sbin/ip", args);
}
До C ++ 17 вы можете использовать &status[0]
вместо status.data()
, что правильно, даже если status.size() == 0
.