Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>char* text = "Hello!"</code> can be thought of as the following:</p> <p>At program start, you create an array of chars, 7 in length: <code>{'H','e','l','l','o','!','\0'}</code>. The last one is the null character and shows that there aren't any more characters after it. [It's more efficient than keeping a count associated with the string... A count would take up perhaps 4 bytes for a 32-bit integer, while the null character is just a single byte, or two bytes if you're using Unicode strings. Plus it's less confusing to have a single array ending in the null character than to have to manage an array of characters and a counting variable at the same time.]</p> <p>The difference between creating an array and making a string constant is that an array is editable and a string constant (or 'string literal') is not. Trying to set a value in a string literal causes problems: they are read-only.</p> <p>Then, whenever you call the statement <code>char* text = "Hello!"</code>, you take the address of that initial array and stick it into the variable <code>text</code>. Note that if you have something like this...</p> <pre><code>char* text1 = "Hello!"; char* text2 = "Hello!"; char* text3 = "Hello!"; </code></pre> <p>...then it's quite possible that you're creating three separate arrays of <code>{'H','e','l','l','o','!','\0'}</code>, so it would be more efficient to do this...</p> <pre><code>char* _text = "Hello!"; char* text1 = _text; char* text2 = _text; char* text3 = _text; </code></pre> <p>Most compilers are smart enough to only initialize one string constant automatically, but some will only do that if you manually turn on certain optimization features.</p> <p>Another note: from my experience, using <code>delete []</code> on a pointer to a string literal doesn't cause issues, but it's unnecessary since as far as I know it doesn't actually delete it.</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. 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