{"id":6395,"date":"2024-05-11T00:01:23","date_gmt":"2024-05-11T07:01:23","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6395"},"modified":"2024-08-31T08:49:50","modified_gmt":"2024-08-31T15:49:50","slug":"messing-with-array-subscripts","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6395","title":{"rendered":"Messing with Array Subscripts"},"content":{"rendered":"<p>Array notation involves the array&#8217;s name followed by a set of square brackets. Within these brackets is a reference to the array element&#8217;s number. It&#8217;s important to remember that the first element is zero. But the subscript need not always be a constant integer.<br \/>\n<!--more--><br \/>\nYes, I know: When the brackets are empty, they refer to the array as a whole: <code>a[]<\/code>. Technically, this notation refers to the array&#8217;s base, though you can&#8217;t use <code>a[]<\/code> as a replacement for the first element individually as it&#8217;s not a single variable. Otherwise, what lies betwixt the brackets is an expression representing an array element.<\/p>\n<p>Beginners code C using constants in the brackets: <code>a[0]<\/code>, <code>a[1]<\/code>, and so on. If you&#8217;re a programmer who mutters as you code, note that <code>a[0]<\/code> is pronounced &#8220;a sub zero.&#8221; The value between the brackets is the subscript.<\/p>\n<p>You can also use variables: <code>a[i]<\/code>, which is muttered as, &#8220;a sub I.&#8221; In this instance, the value of variable <code>i<\/code> references an element.<\/p>\n<p>The subscript can be any valid expression in C the results in an integer. For example:<\/p>\n<p><code>a[i+2]<\/code><\/p>\n<p>The element referenced is the result of the value of variable <code>i<\/code> plus two. Or you could do math with two separate variables:<\/p>\n<p><code>a[x\/y]<\/code><\/p>\n<p>The element referenced is the value of variable <code>x<\/code> divided by variable <code>y<\/code>. The result is rounded to the nearest integer, which isn&#8217;t precise so this example is iffy.<\/p>\n<p>How about this:<\/p>\n<p><code>a[funct()]<\/code><\/p>\n<p>Yes, you can use the return value from a function between the brackets, providing that the function returns an integer value. Functions are considered expressions in this manner.<\/p>\n<p>Here is code I threw together to show the various ways you can mess with an array&#8217;s subscript:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05_11-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_05_11-Lesson.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[5] = { 10, 20, 30, 40, 50 };\r\n\r\n    printf(\"%d\\n\",a[0]);\r\n    printf(\"%d\\n\",a[0+1]);\r\n    printf(\"%d\\n\",a[0+1+1]);\r\n    printf(\"%d\\n\",a[0+1+1+1]);\r\n    printf(\"%d\\n\",a[0+1+1+1+1]);\r\n\r\n    int x = 30;\r\n    int y = 10;\r\n    printf(\"%d\\n\",a[x\/y]);\r\n\r\n    srand( (unsigned)time(NULL) );\r\n    printf(\"%d\\n\",a[rand()%5]);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Array <code>a[]<\/code> is an integer array with five values, elements zero through four.<\/p>\n<p>The first few <em>printf()<\/em> statements use addition to output each subsequent element. In a way, this format relates to how a pointer references offsets in a memory buffer, as in: <code>*(a+1)<\/code><\/p>\n<p>The various addition examples are a bit silly as they involve too much typing; a programmer would simplify the math. Here I&#8217;m showing that you don&#8217;t need to simplify as the expression is evaluated by the compiler.<\/p>\n<p>The next <em>printf()<\/em> statement divides two variables, <code>x<\/code> and <code>y<\/code>, to obtain the element number.<\/p>\n<p>Finally, the <em>rand()<\/em> function returns a random value, which is modulo-ed to an integer between zero and five. This approach is perfectly legit.<\/p>\n<p>The code builds and the program runs. Here&#8217;s sample output:<\/p>\n<p><code>10<br \/>\n20<br \/>\n30<br \/>\n40<br \/>\n50<br \/>\n40<br \/>\n50<\/code><\/p>\n<p>I don&#8217;t expect anyone to use such subscripts deliberately, but these options demonstrate what&#8217;s possible. And if you&#8217;re into obfuscation, they provide yet another vehicle to add a bit of mirth and magic to your C source code.<\/p>\n<p>Also see <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6540\">this post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The element number inside an array&#8217;s brackets need not be a constant value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6395\">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-6395","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\/6395","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=6395"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6395\/revisions"}],"predecessor-version":[{"id":6561,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6395\/revisions\/6561"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}