Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The expression <code>x[y]</code> is equivalent to <code>*(x+y)</code> hence it makes no difference whether you state it as <code>x[y]</code> or <code>y[x]</code>.</p> <p>In fact, <code>"0123456789abcdef"[nybble % 0x10]</code> is also a valid expression, returning one of those characters depending on the value within the brackets. So is the alternative, as shown in this program:</p> <pre><code>#include &lt;stdio.h&gt; int main() { char *arr = "0123456789abcdef"; int nybble = 11; printf ("%c%c\n", arr[11], 11[arr]); return 0; } </code></pre> <p>which prints <code>b</code> twice. So, no, <code>c</code> doesn't have to be an array at all.</p> <p>In terms of why it's always true, just convert them to the <code>*(x+y)</code> form:</p> <pre><code> a[b[c]] == c[b][a] =&gt; a[*(b + c)] == (*(c + b))[a]) =&gt; *(a + *(b + c)) == *(*(c + b) + a) </code></pre> <p>Now, because <code>a + *(b + c)</code> is mathematically exactly the same as <code>*(c + b) + a</code>, both sides of the equation reference the same memory location, hence the expression will always be true.</p> <hr> <p>Now keep in mind that the compiler <em>may</em> be smart enough to figure out that it's the same thing and optimise it to always give you true, but you're still straying into undefined behaviour territory.</p> <p>Because you don't explicitly set <code>c</code> to a value, and it's not of static storage duration, it will be given an arbitrary value. That means that you may actually use an idex into the <code>b</code> array that's beyond the end of the actual array, a big no-no in C circles.</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