Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><strong>Why are strings in C++ usually terminated with <code>'\0'</code>?</strong></p> </blockquote> <p>Note that C++ Strings and C strings are not the same.<br> In C++ string refers to <strong><a href="http://www.cplusplus.com/reference/string/string/" rel="noreferrer">std::string</a></strong> which is a template class and provides a lot of intuitive functions to handle the string.<br> Note that C++ std::string are not <code>\0</code> terminated, but the class provides functions to fetch the underlying string data as <code>\0</code> terminated c-style string.</p> <p>In C a string is collection of characters. This collection usually ends with a <code>\0</code>.<br> Unless a special character like <code>\0</code> is used there would be no way of knowing when a string ends.<br> It is also aptly known as the string null terminator. </p> <p>Ofcourse, there could be other ways of bookkeeping to track the length of the string, but using a special character has two straight advantages: </p> <ul> <li>It is more intuitive and </li> <li>There are no additional overheads</li> </ul> <p>Note that <code>\0</code> is needed because most of Standard C library functions operate on strings assuming they are <code>\0</code> terminated.<br> For example:<br> While using <code>printf()</code> if you have an string which is not <code>\0</code>terminated then <code>printf()</code> keeps writing characters to <code>stdout</code> until a <code>\0</code> is encountered, in short it might even print garbage. </p> <blockquote> <p><strong>Why should we use <code>'\0'</code> here?</strong></p> </blockquote> <p>There are two scenarios when you do not need to <code>\0</code> terminate a string:</p> <ul> <li>In any usage if you are explicitly bookkeeping length of the string and </li> <li>If you are using some standard library api will implicitly add a <code>\0</code> to strings. </li> </ul> <p>In your case you already have the second scenario working for you.</p> <pre><code>array[s.size()] = '\0'; </code></pre> <p>The above code statement is redundant in your example. </p> <p>For your example using <code>strncpy()</code> makes it useless. <code>strncpy()</code> copies <code>s.size()</code> characters to your <code>array</code>, Note that it appends a null termination if there is any space left after copying the strings. Since <code>array</code>is of size <code>s.size() + 1</code> a <code>\0</code> is automagically added. </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