Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like you understand what C-style strings are, but to summarize, they are just arrays of characters in memory, by convention terminated by a nul character <code>\0</code>. Usually they are referenced via a <code>char*</code> pointing to the first letter in the string. When they are printed, typically the characters of the string are printed starting from the first, and printing (or copying, etc.) stops when the <code>\0</code> terminator is reached.</p> <p>An <code>std::string</code> is a class that (typically) wraps a C-style string. This means that a <code>std::string</code> object (typically) has a private C-style string that is used to implement its functionality. The function <code>std::string::c_str()</code> returns a pointer to this underlying C-style string.</p> <p>Let's suppose that <code>char *str;</code> points to a C-style string. If you attempt to run <code>cout &lt;&lt; *str &lt;&lt; endl;</code>, you noticed that only the first character is printed. That is because of C++'s function overloading. The data type of <code>*str</code> is <code>char</code>, so the <code>char</code> version of <code>cout</code> is called and faithfully prints the single character <code>*str</code>. For compatibility with C-style strings, the version of <code>cout</code> that takes a <code>char*</code> as an argument treats the pointer as a C-style string for printing purposes. If you <code>cout</code> an <code>int*</code>, for example, the underlying <code>int</code> will not be printed.</p> <p><strong>Edit:</strong> Another comment:</p> <p>The reason that your attempt to dereference an <code>std::string</code> object failed is that, indeed, it is not a pointer. You could dereference the return value of <code>std::string::c_str()</code>, and you would get back the first <code>char</code> of the string.</p> <p>Related: <a href="https://stackoverflow.com/questions/1466073/how-is-stdstring-implemented">How is std::string implemented?</a>.</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. 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