Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to pass vector of string to foo(char const *const *const)?
    primarykey
    data
    text
    <p><strong>Edit:</strong> After implementing James Hopkin' suggestion, I still get the warnings of "invalid name 'null'", which is much better than those weird characters. Then, I went back and read the library document again, and it turns out that for that particular function, it's argument should have one more element of non-NULL string than the size of aNames. That additional string has some other purpose. After adding one more string, the code runs fine. It's all my fault, and I am so sorry.</p> <p>Original post:</p> <p>Hi, </p> <p>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.</p> <p>I need to pass a vector of string to a library function foo(char const *const *const). I can not pass the &amp;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):</p> <pre><code>const char* aR[aNames.size()]; std::transform(aNames.begin(), aNames.end(), aR, boost::bind(&amp;std::string::c_str, _1)); foo(aR); </code></pre> <p>However, it seems it causes some undefined behavior:</p> <p>If I run the above code, then the function foo throw some warnings about illegal characters ('èI' blablabla) in aR.</p> <p>If I print aR before function foo like this:</p> <pre><code>std::copy(aR, aR+rowNames.size(), std::ostream_iterator&lt;const char*&gt;(std::cout, "\n")); foo(aR); </code></pre> <p>Then, everything is fine. My questions are:</p> <ol> <li><p>Does the conversion causes undefined behavior? If so, why?</p></li> <li><p>What is the correct way to pass vector of string to foo(char const *const *const)?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload