{"id":344,"date":"2015-11-17T15:48:16","date_gmt":"2015-11-17T19:48:16","guid":{"rendered":"http:\/\/www.tayloraliss.com\/blog\/?p=344"},"modified":"2015-11-17T23:27:10","modified_gmt":"2015-11-18T03:27:10","slug":"references-pointers","status":"publish","type":"post","link":"https:\/\/www.tayloraliss.com\/blog\/?p=344","title":{"rendered":"References &#038; Pointers"},"content":{"rendered":"<p>This week our class has finally encountered pointers. I knew that it would be a challenging subject, so I&#8217;m trying to take it slow. There are a couple of questions I have been trying to resolve in my mind since encountering these two concepts. The first one is: what is the point of pointers?<\/p>\n<p>That&#8217;s where I discovered\u00a0<a href=\"https:\/\/www.reddit.com\/r\/learnprogramming\/comments\/1w6v07\/eli5_pointers_and_their_usefulness\/cez9ijk\">this comment<\/a>\u00a0on reddit that I feel applies to both pointers and references:<\/p>\n<blockquote><p>The best metaphor I&#8217;ve found for pointers are websites.<\/p>\n<p>You could copy and paste the contents of a website into a document and then send that to someone, or you can send a pointer (the URL) to them.<\/p>\n<p>The benefit of the pointer is if the site gets updated, you both see the changes, whereas if you send a pasted document, they will never see any changes.<\/p><\/blockquote>\n<p>If you just use the variable, it&#8217;s copying the whole contents of the variable. Whereas if you use a pointer\/reference, it&#8217;s sending a lot less data and using a lot less memory.<\/p>\n<p>Another\u00a0difficult questions I&#8217;ve been working on answering is what the difference is between pointers and references. For that answer, I went to <a href=\"http:\/\/stackoverflow.com\/a\/7058373\">StackOverflow<\/a>:<\/p>\n<blockquote><p>Use reference wherever you can, pointers wherever you must.<\/p>\n<p>Avoid pointers until you can&#8217;t.<\/p>\n<p>The reason is that pointers make things harder to follow\/read, less safe and far more dangerous manipulations than any other constructs.<\/p>\n<p>So the rule of thumb is to use pointers only if there is no other choice.<\/p><\/blockquote>\n<p>I&#8217;m sure there is more to it than that, but feel that those cover the basics pretty well. Now for actually learning the syntax. I wrote a little program to help remind myself of the finer points:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\"> int myInt;\r\n int *myPointer;\r\n myPointer = &amp;myInt;\r\n *myPointer = 5;\r\n cout &lt;&lt; \"myPointer points to address \" &lt;&lt; myPointer &lt;&lt; endl; \/\/outputs address\r\n cout &lt;&lt; \"The value stored at that address is \" &lt;&lt; *myPointer &lt;&lt; endl; \/\/outputs 5\r\n\r\n \/\/An array name, without brackets and a subscript, actually represents the starting \r\n \/\/address of the array. This means that an array name is really a pointer.\r\n int myArray[] = { 10, 20, 30 };\r\n cout &lt;&lt; *myArray &lt;&lt; endl; \/\/outputs 10\r\n\r\n \/\/These two lines do the same thing:\r\n cout &lt;&lt; myArray[1] &lt;&lt; endl; \/\/outputs 20\r\n cout &lt;&lt; *(myArray + 1) &lt;&lt; endl; \/\/outputs 20\r\n\r\n \/\/This adds 1 to the first value in the array:\r\n cout &lt;&lt; *myArray + 1 &lt;&lt; endl; \/\/outputs 11\r\n\r\n int *arrayPoint; \/\/This is a pointer that points to an int (or an array of ints)\r\n arrayPoint = myArray; \/\/Assign it to an array to point to\r\n cout &lt;&lt; *arrayPoint &lt;&lt; endl; \/\/outputs 10\r\n arrayPoint++;\r\n cout &lt;&lt; *arrayPoint &lt;&lt; endl; \/\/outputs 20<\/pre>\n<p>I&#8217;ll add some more notes for pointers and references later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week our class has finally encountered pointers. I knew that it would be a challenging subject, so I&#8217;m trying to take it slow. There are a couple of questions I have been trying to resolve in my mind since encountering these two concepts. The first one is: what is the point of pointers? That&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344"}],"version-history":[{"count":10,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/344\/revisions"}],"predecessor-version":[{"id":359,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/344\/revisions\/359"}],"wp:attachment":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}