Dealing with char arrays in C++
<p>I have this C-styled piece of initialization code:</p>

<pre><code>const char * const vlc_args[] =
{
    "-I", "dummy",
    "--ignore-config",
    "--extraintf=logger",
    "--verbose=2"
    "--plugin-path=/usr/lib/vlc"
};
//tricky calculation of the char space used
libvlc_new(sizeof(vlc_args)/sizeof(vlc_args[0]), vlc_args, &amp;exc);
</code></pre>

<p>Since I need to make the <code>--plugin-path</code> parameter dynamic, I can't use a static array anymore. So I came up with a C++ alternative:</p>

<pre><code>std::string pluginpath = "test";
libvlc_exception_t exc;

std::vector&lt;std::string&gt; args;
args.push_back("-I");
args.push_back("dummy");
args.push_back("--ignore-config");
args.push_back("--extraintf=logger");
args.push_back("--verbose=2");
args.push_back("--ipv4");
args.push_back("--plugin-path=" + pluginpath);

std::string combinedString;
for (size_t idx = 0; idx &lt; args.size(); ++idx)
{
    combinedString.append(args[idx]);
    combinedString.resize(combinedString.size() + 1);
    combinedString[combinedString.size() - 1] = 0;
}
combinedString.resize(combinedString.size() + 1);
combinedString[combinedString.size() - 1] = 0;

size_t size = combinedString.size();
const char * data = combinedString.c_str();
libvlc_new(size, &amp;data, &amp;exc); // =&gt; error occurs here (not at end of scope or anything)
</code></pre>

<p>But this results in a segmentation fault. So there must be an error in my code, which I can't seem to find.. Can anyone spot it?</p>

<p><b>Solved!</b><br>
Thanks to Joseph Grahn and Jason Orendorff. My idea on the memory layout of the C-style array was wrong. I thought all data was organized as a big sequential block. In reality it's a list of pointers to the first character of each individual string.</p>

<p>This code works:</p>

<pre><code>std::vector&lt;const char*&gt; charArgs;
for (size_t idx = 0; idx &lt; args.size(); ++idx)
{
    charArgs.push_back(&amp;(args[idx][0]));
}
mVLCInstance = libvlc_new(charArgs.size(),
                          &amp;charArgs[0],
                          &amp;mVLCException);
</code></pre>
