how to pass vector of string to foo(char const *const *const)?

Posted by user347208 on Stack Overflow See other posts from Stack Overflow or by user347208
Published on 2010-05-21T15:57:00Z Indexed on 2010/05/21 16:00 UTC
Read the original article Hit count: 154

Filed under:
|
|
|

Hi,

This is my first post so please be nice. I searched in this forum and googled but I still can not find the answer. This problem has bothered me for more than a day, so please give me some help. Thank you.

I need to pass a vector of string to a library function foo(char const *const *const). I can not pass the &Vec[0] since it's a pointer to a string. Therefore, I have an array and pass the c_str() to that array. The following is my code (aNames is the vector of string):

const char* aR[aNames.size()];

std::transform(aNames.begin(), aNames.end(), aR, 
                boost::bind(&std::string::c_str, _1));
foo(aR);

However, it seems it causes some undefined behavior:

If I run the above code, then the function foo throw some warnings about illegal characters ('èI' blablabla) in aR.

If I print aR before function foo like this:

std::copy(aR, aR+rowNames.size(), 
          std::ostream_iterator<const char*>(std::cout, "\n"));
foo(aR);

Then, everything is fine. My questions are:

  1. Does the conversion causes undefined behavior? If so, why?

  2. What is the correct way to pass vector of string to foo(char const *const *const)?

Thank you very much for your help!

© Stack Overflow or respective owner

Related posts about c++

Related posts about vector