{"id":6540,"date":"2024-08-31T00:01:18","date_gmt":"2024-08-31T07:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6540"},"modified":"2024-08-24T10:28:37","modified_gmt":"2024-08-24T17:28:37","slug":"more-messing-with-array-subscripts","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6540","title":{"rendered":"More Messing with Array Subscripts"},"content":{"rendered":"<p>I&#8217;ve <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6395\">written before<\/a> about array subscripts and how they&#8217;re secretly expressions. But another weird aspect of array subscripts also looms on the horizon: The format <code>a[1]<\/code> can be written as <code>1[a]<\/code>.<br \/>\n<!--more--><br \/>\nBefore I explain what&#8217;s going on, here&#8217;s sample code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_08_31-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_08_31-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a[] = { 1, 2, 3, 4, 5 };\r\n\r\n    printf(\"%d\\n\",a[2]);\r\n    printf(\"%d\\n\",2[a]);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The two <em>printf()<\/em> statements use both forms to access the third array element, traditionally written as <code>a[2]<\/code>. Here&#8217;s the output:<\/p>\n<p><code>3<br \/>\n3<\/code><\/p>\n<p>Both arrangements generate the same result, accessing the third element in array <code>a[]<\/code>. But how?<\/p>\n<p>You&#8217;re probably aware of the weird relationship between pointers and arrays. They aren&#8217;t the same thing; array notation isn&#8217;t shorthand for pointer notation. The big difference is how the compiler stores the data in memory. Regardless, internally the compiler uses pointer offsets to access array elements.<\/p>\n<p>For example, <code>a[2]<\/code> translates into <code>*(a+2)<\/code>. Because this translation takes each part of the array notation and shoves it into the pointer format, when you write <code>2[a]<\/code> you get <code>*(2+a)<\/code>.<\/p>\n<p>Yes, the expression <code>*(2+a)<\/code> looks awkward, but the math is the same as <code>*(a+2)<\/code>. You can use either format to obtain the same result.<\/p>\n<p>The weirder thing, which harkens to my earlier blog post, is whether this format works with expressions. Here&#8217;s the code I used to test:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_08_31-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_08_31-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int a[] = { 10, 20, 30, 40, 50 };\r\n\r\n    srand( (unsigned)time(NULL) );\r\n    printf(\"%d\\n\", (rand()%5)[a] );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code updates code presented in the Lesson from May. The expression <code>rand()%5<\/code> generates a random value from 0 through 4. It&#8217;s prefixed before the brackets with the array name inside the brackets. The expression is enclosed in parentheses, otherwise the compiler would try to fetch the non-existent fifth element from array <code>a[]<\/code>. (The order of operations binds the 5 tight to the brackets.)<\/p>\n<p>The output shows a random element from the array, just as if the expression <code>rand()%5<\/code> were in the brackets. So it works!<\/p>\n<p>While I&#8217;m delighted in the funky nature of swapping an array name with its element number (or expression), I would never use such a format in my code. Yes, it&#8217;s wonderfully cryptic. Obfuscators would enjoy using this trick, but I aim for readable code.<\/p>\n<p>A bigger question, one that I haven&#8217;t attempted, is whether this trick works in Python or other languages. It&#8217;s not that using the backwards format does anything special, just that such things are entertaining to nerds such as myself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s something truly weird that works &mdash; and makes sense when you know what&#8217;s going on behind the scenes. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6540\">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-6540","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\/6540","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=6540"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6540\/revisions"}],"predecessor-version":[{"id":6558,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6540\/revisions\/6558"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}