Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This declaration:</p> <pre><code>char s[] = "hello"; </code></pre> <p>Creates <em>one</em> object - a <code>char</code> array of size 6, called <code>s</code>, initialised with the values <code>'h', 'e', 'l', 'l', 'o', '\0'</code>. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will live until the end of the block that it is declared in, and almost certainly be allocated on the stack; if it's outside a function, it will <em>probably</em> be stored within an "initialised data segment" that is loaded from the executable file into writeable memory when the program is run.</p> <p>On the other hand, this declaration:</p> <pre><code>char *s ="hello"; </code></pre> <p>Creates <em>two</em> objects:</p> <ul> <li>a <strong>read-only</strong> array of 6 <code>char</code>s containing the values <code>'h', 'e', 'l', 'l', 'o', '\0'</code>, which has no name and has <em>static storage duration</em> (meaning that it lives for the entire life of the program); and</li> <li>a variable of type pointer-to-char, called <code>s</code>, which is initialised with the location of the first character in that unnamed, read-only array.</li> </ul> <p>The unnamed read-only array is typically located in the "text" segment of the program, which means it is loaded from disk into read-only memory, along with the code itself. The location of the <code>s</code> pointer variable in memory depends on where the declaration appears (just like in the first example).</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