Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The declaration and initialization</p> <pre><code>char *array = "One good thing about music"; </code></pre> <p>declares a pointer <code>array</code> and make it point to a constant array of 31 characters.</p> <p>The declaration and initialization</p> <pre><code>char array[] = "One, good, thing, about, music"; </code></pre> <p>declares an array of characters, containing 31 characters.</p> <p>And yes, the size of the arrays is 31, as it includes the terminating <code>'\0'</code> character.</p> <hr> <p>Laid out in memory, it will be something like this for the first:</p> <pre> +-------+ +------------------------------+ | array | --> | "One good thing about music" | +-------+ +------------------------------+ </pre> <p>And like this for the second:</p> <pre> +------------------------------+ | "One good thing about music" | +------------------------------+ </pre> <hr> <p>Arrays decays to pointers to the first element of an array. If you have an array like</p> <pre><code>char array[] = "One, good, thing, about, music"; </code></pre> <p>then using plain <code>array</code> when a pointer is expected, it's the same as <code>&amp;array[0]</code>.</p> <p>That mean that when you, for example, pass an array as an argument to a function it will be passed as a pointer.</p> <p>Pointers and arrays are <em>almost</em> interchangeable. You can not, for example, use <code>sizeof(pointer)</code> because that returns the size of the actual pointer and not what it points to. Also when you do e.g. <code>&amp;pointer</code> you get the address of the pointer, but <code>&amp;array</code> returns a pointer to the array. It should be noted that <code>&amp;array</code> is <em>very</em> different from <code>array</code> (or its equivalent <code>&amp;array[0]</code>). While both <code>&amp;array</code> and <code>&amp;array[0]</code> point to the same location, the types are different. Using the arrat above, <code>&amp;array</code> is of type <code>char (*)[31]</code>, while <code>&amp;array[0]</code> is of type <code>char *</code>.</p> <hr> <p>For more fun: As many knows, it's possible to use array indexing when accessing a pointer. But because arrays decays to pointers it's possible to use some pointer arithmetic with arrays.</p> <p>For example:</p> <pre><code>char array[] = "Foobar"; /* Declare an array of 7 characters */ </code></pre> <p>With the above, you can access the fourth element (the <code>'b</code>' character) using either</p> <pre><code>array[3] </code></pre> <p>or</p> <pre><code>*(array + 3) </code></pre> <p>And because addition is <a href="http://en.wikipedia.org/wiki/Commutativity" rel="noreferrer">commutative</a>, the last can also be expressed as</p> <pre><code>*(3 + array) </code></pre> <p>which leads to the fun syntax</p> <pre><code>3[array] </code></pre>
 

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