Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you do</p> <pre><code>char* something = "a string literal"; </code></pre> <p>The compiler puts <code>"a string literal"</code> into the executable image itself and just assigns a pointer to this memory to <code>something</code>. You are not allowed to modify this memory, and many times the memory that the string resides in is marked read-only, so any attempts to write to it result in an access violation like the one you experienced.</p> <p>When you do </p> <pre><code>char something[] = "a string literal"; </code></pre> <p>you are really creating an array on the stack named <code>something</code> and <em>initialising</em> it with <code>"a string literal"</code>. This is equivalent of doing <code>char something[] = {'a', ' ', 's', 't', 'r', ..., 'a', 'l', 0};</code>. Since this memory is on the stack, you can modify it freely.</p> <p><code>char* something = "a string literal"</code> looks like</p> <pre><code> stack executable ------------- --------------------- |~~~~~~~~~~~| | ~~~~~~~~~~~~~~~~~ | | something | -----------------&gt; | a string literal0 | ------------- | ~~~~~~~~~~~~~~~~~ | --------------------- </code></pre> <p>Whereas <code>char something[] = "a string literal"</code> looks like</p> <pre><code>stack ----- |~~~| | a | &lt;- something is an alias for this location | | | s | | t | | r | | i | | n | | g | | | | l | | i | | t | | e | | r | | a | | l | | 0 | ----- </code></pre> <p>Where <code>~~~</code> means "other memory etc".</p> <p>Note that</p> <pre><code>char* x = "string literal"; </code></pre> <p>Is actually invalid and should not compile because you cannot convert a <code>char const[x]</code> to a <code>char*</code>. It should be <code>const char* x</code>, not <code>char* x</code>, but some old and non-conformant compilers wrongly allow this behaviour.</p>
 

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