{"id":3517,"date":"2019-03-02T00:01:29","date_gmt":"2019-03-02T08:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3517"},"modified":"2019-02-16T15:57:07","modified_gmt":"2019-02-16T23:57:07","slug":"a-curious-thing-about-array-notation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3517","title":{"rendered":"A Curious Thing About Array Notation"},"content":{"rendered":"<p>It&#8217;s nuts. All through your C training when you learned about arrays, you were taught the format: <code>array[x]<\/code> where <code>array<\/code> is the name of the array and <code>x<\/code> is the element number. But this expression can also be written as <code>x[array]<\/code>.<\/p>\n<p>Mind. Blown.<br \/>\n<!--more--><br \/>\nThis oddity is related to how arrays are dealt with internally by the compiler. As I&#8217;ve written about in my books and described in my online training, there is a strong relationship between arrays and pointers. It&#8217;s often said that array notation is a shortcut for pointers, though that&#8217;s not 100 percent accurate. Instead, it&#8217;s better to say that arrays translate into pointers easily. And I&#8217;ll add that using pointers is faster. Anyway.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a[5] = { 10, 20, 30, 40, 50 };\r\n\r\n    printf(\"%d\\n\",a[3]);\r\n    printf(\"%d\\n\",3[a]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<p><code>40<br \/>\n40<\/code><\/p>\n<p>Both expressions <code>a[3]<\/code> and <code>3[a]<\/code> reference the fourth element of array <code>a[]<\/code>. Upon learning such thing I freaked out. &#8220;What if I&#8217;ve been doing it wrong!&#8221;<\/p>\n<p>Don&#8217;t worry: I must maintain that proper and consistent representation of an array in the C language is important. In the original K&#038;R manual, arrays are written <code><em>name<\/em>[<em>size<\/em>]<\/code>. That&#8217;s it and that&#8217;s how everyone should do it. In fact, you cannot declare an array like this:<\/p>\n<p><code>int 5[a];<\/code><\/p>\n<p>This statement generates a host of errors should you attempt to compile the code. But the expression <code>3[a]<\/code> does in fact represent the fourth element of the array. How?<\/p>\n<p>Internally, the compiler sees the name of the array as a base address. It&#8217;s not really a pointer, but the <code>a<\/code> in <code>a[3]<\/code> refers to a memory location. It&#8217;s similar to the pointer expression <code>*(a+3)<\/code>.<\/p>\n<p>When you reference an array element as <code>3[a]<\/code>, internally the compiler casts the expression as <code>*(3+a)<\/code>. Therefore:<\/p>\n<p><code>a[3] == 3[a]<\/code> because <code>*(a+3) == *(3+a)<\/code><\/p>\n<p>I hope my explanation removes any puzzlement you may have over the <code>3[a]<\/code> type of expression. In practical use, I strongly recommend that you write <code>a[3]<\/code> to keep your code consistent and readable. But you ever stumble across some joker who reverses his array notation for whatever reason, now you know he&#8217;s just being goofy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prepare to completely lose focus on what you thought was array notation. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3517\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3517","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3517"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3517\/revisions"}],"predecessor-version":[{"id":3525,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3517\/revisions\/3525"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}