-1

I have an array of strings and I want to use the elements of that array as the argument of a function, but the argument of my function should be char* type.

How can I solve this problem?

string Arr[]={"aa.txt","bb.txt","cc.txt"};
void myfunc(char* Arr[i]);

I am programming in visual studio C++ 2010.

Mogsdad
  • 40,814
  • 19
  • 140
  • 246
  • possible duplicate of [How to convert string to char array in C++?](http://stackoverflow.com/questions/13294067/how-to-convert-string-to-char-array-in-c) – alesegdia Sep 14 '15 at 15:04
  • [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – R Sahu Sep 14 '15 at 15:04
  • 1
    I recommend changing the parameter type to a `vector` of `string`. The function parameter is a pointer to a **single** character. You could also change the parameter to `std::string`. You should think twice about converting between `std::string` and `char *`. Always prefer the `string`. – Thomas Matthews Sep 14 '15 at 15:13

1 Answers1

0

Use std::string::c_str(), but the function argument would need to be const. Otherwise, I think your only option is to alloc a copy of the returned const char* std::string::c_str().

alesegdia
  • 518
  • 3
  • 14