简体   繁体   中英

Convert from string array to char* const

I'm looking for a way to call execvp() in a C++ program that uses string arrays. So if, for example, I have an array of strings,

s[0] = "ls";
s[1] = "-l";

then, s[i].c_str() converts it to const char* . However, I need s[i] converted to a char* const to pass it to execvp() . Is there any way to do this in C++?

I was able to answer my own question. I had to convert the const char* to a char* .

However, since C++ doesn't allow conversion from const char* to char* , I had to create an extern C function. I converted the string s to a C-string (as a const char* ), passed it to the extern C function. Within the C function I could then convert it from a const char* to a char* and subsequently pass it to execvp() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM