{"id":399,"date":"2013-11-23T00:01:48","date_gmt":"2013-11-23T08:01:48","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=399"},"modified":"2013-11-09T09:14:40","modified_gmt":"2013-11-09T17:14:40","slug":"array-zing-hell","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=399","title":{"rendered":"Array Zing Hell"},"content":{"rendered":"<p>An array is a queue of values, all stored as one compact unit with a handy reference method. As you study arrays, especially when you get into pointers, you discover something interesting about the array&#8217;s first element.<br \/>\n<!--more--><br \/>\nSpecifically, you find out that the array&#8217;s address &#8212; its location in memory &#8212; is the same as the address of the first element. For some reason this relationship causes massive confusion, maybe even convulsions.<\/p>\n<p>To review, take your typical array. In this case, I&#8217;m concocting an integer array named <code>doris<\/code>, as shown in the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int doris[] = { 2, 3, 5, 8, 13 };\r\n\r\n    printf(\"%p array address\\n\",&doris);\r\n    printf(\"%p first element\\n\",&doris[0]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>doris<\/code> array has five elements. They&#8217;re stored in memory, one after the other, each given their own specific address and a proper number of bytes according to the <em>sizeof<\/em> an integer on whatever gizmo the code is compiled.<\/p>\n<p>Lines 7 and 8 in the code display the address of <code>doris<\/code> itself (herself?) as well as the address of the array&#8217;s first element, <code>doris[0]<\/code>. Here&#8217;s the output on my computer:<\/p>\n<pre><code>0x7fff59a7fa94 array address\r\n0x7fff59a7fa94 first element<\/code><\/pre>\n<p>As you can see, both addresses are the same. The start of the array itself is at memory location <code>...fa94<\/code> and the first element is at <code>...fa94<\/code>. Identical.<\/p>\n<p>That location makes sense to me. After all, the array starts at the first element. Why would its location be elsewhere?<\/p>\n<p>What I believe is throwing some readers is that the two items being referenced are not the same. In this case, <code>doris<\/code> is an array. The compiler actually recognizes it as a memory location, a pointer. The array notation is merely window dressing. In fact, you can change Line 7 in the code to read as follows:<\/p>\n<p><code>printf(\"%p array address\\n\",doris);<\/code><\/p>\n<p>See how the <code>&<\/code> is missing before the variable <code>doris<\/code>?<\/p>\n<p>The output is the same. Again, that&#8217;s because <code>doris<\/code> is really a pointer. The compiler sees the <code>&<\/code> unary operator there, shrugs its shoulders, and says &#8220;Meh.&#8221; The <code>&<\/code> is superfluous. You don&#8217;t see an error message, which may be confusing, because there is no error. Both <code>&doris<\/code> and <code>doris<\/code> are an address. The <code>&<\/code> operator doesn&#8217;t get &#8220;the address of an address&#8221; because there is no such thing.<\/p>\n<p>But what about <code>&doris[0]<\/code>?<\/p>\n<p>That&#8217;s a different creature: The variable <code>doris<\/code> is a pointer. The variable <code>doris[0]<\/code> is an <em>int<\/em>.<\/p>\n<p>The variables <code>doris[1]<\/code>, <code>doris[2]<\/code>, <code>doris[3]<\/code>, and <code>doris[4]<\/code> are also <em>ints<\/em>. They are not pointers. They hold integer values, not  memory locations (addresses). Therefore, to fetch the address of any array element, you definitely need the <code>&<\/code> unary operator.<\/p>\n<p>In the grand scheme of things, the array&#8217;s first element, <code>doris[0]<\/code> (which is an <em>int<\/em> value), is located as the same address as <code>doris<\/code>, the start of the array. Therefore, generically speaking, the memory location stored in <code>array<\/code> is always the same as the address of <code>array[0]<\/code>, which is coded as <code>&array[0]<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first element of an array holds the same address as the array itself. So what&#8217;s the difference? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=399\">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-399","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\/399","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=399"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions\/405"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}