0

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++?

3
  • and must be sure, that std::string pointer is taken, exist to the last moment of algorithm using pointer (often error: pointer from variable on the stack) Commented Feb 1, 2017 at 21:09
  • or make copies of the strings Commented Feb 1, 2017 at 22:43
  • const_cast doesn't work for const char* to char* const. How will making copies of the string help? Commented Feb 2, 2017 at 21:07

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.