Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even though <code>strcat()</code> is probably the best solution as already mentioned by ScarletAmanranth, there is also another one.</p> <p>One can also convert the c-strings to <code>std::string</code>'s first, perform any desired operations on them and then convert the result back to a c-string. This might better suit your needs in some cases.</p> <p><strong>Example:</strong></p> <pre><code>const char* str1 = "foo"; const char* str2 = "bar"; std::string combined = std::string(str1) + str2; // notice std::string + char* =&gt; std::string const char* result = combined.c_str(); </code></pre> <p><strong>Advantages:</strong></p> <ul> <li>Can be written as oneliner, without any further delcarations of buffers etc.</li> <li>Might allow easier manipulation through <code>std::string</code> methods and related functions</li> </ul> <p><strong>Drawbacks:</strong></p> <ul> <li>Additional overhead</li> <li>Only a <code>const char*</code> can be acquired, if a <code>char*</code> is required one must copy the c-string again</li> </ul> <p>Consider that the returned <code>const char*</code> directly points to the internal buffer of the <code>std::string</code> object. Thus the pointer only remains valid as long as the <code>std::string</code> object does not run out-of-scope. Therefor using this with a temporary like this <code>const char* result = (std::string(str1) + str2).c_str();</code> is a very bad idea (whereas it would be legit to write someting like that in a function call, as the temporary remains until the call returns).</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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