fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. void legacy_function(char **myList)
  6. {
  7. for (int i = 0; myList[i]; ++i)
  8. std::cout << myList[i] << "\n";
  9. }
  10.  
  11. using namespace std;
  12. int main()
  13. {
  14. vector<string> myVector;
  15. myVector.push_back("first");
  16. myVector.push_back("second");
  17. //...
  18. // create the pointer vector
  19. vector<char *> myPtrVector;
  20. // add pointer to string to vector
  21. for (size_t i = 0; i < myVector.size(); ++i)
  22. myPtrVector.push_back(const_cast<char*>(myVector[i].c_str()));
  23. // stick the null at the end
  24. myPtrVector.push_back(NULL);
  25. // ...
  26.  
  27.  
  28. // call legacy function
  29. legacy_function(&myPtrVector[0]);
  30. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
first
second