{"id":360,"date":"2015-11-23T10:47:59","date_gmt":"2015-11-23T14:47:59","guid":{"rendered":"http:\/\/www.tayloraliss.com\/blog\/?p=360"},"modified":"2015-11-23T11:09:52","modified_gmt":"2015-11-23T15:09:52","slug":"pointers-round-2","status":"publish","type":"post","link":"https:\/\/www.tayloraliss.com\/blog\/?p=360","title":{"rendered":"Pointers:  Round 2"},"content":{"rendered":"<p>Last Saturday I had my final exam for my CS161 course. I did\u00a0<em>okay<\/em>, but I could have done better. I realize now that I really don&#8217;t have a handle on how pointers work still, so the goal of this post is to try and help myself understand them a little bit more.<br \/>\nI was watching some videos from <a href=\"https:\/\/www.youtube.com\/watch?v=_ja8iizm7nk\">The New Boston<\/a>\u00a0when I noticed that in the comments section\u00a0some people were saying that the video had mistakes. It&#8217;s a bit disheartening to hear that the people trying to teach you pointers can&#8217;t even get it right. I&#8217;m going to try and digest what was in the video and what the commentators said. Here&#8217;s my synopsis:<\/p>\n<p>Pass-by-reference uses references, not pointers. Pass-by-<b>address<\/b> uses pointers. Pass-by-reference and pass-by-address are not the same thing. It&#8217;s an important distinction because pass-by-reference is handled differently when calling the function.<\/p>\n<p>A pass-by-reference function using references looks like this: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">passByReference(int &amp;x);<\/code><\/p>\n<p>While a pass-by-<b>address<\/b>\u00a0function using pointers\u00a0looks like this: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">passByAddress(int *x);<\/code><\/p>\n<p>The important difference here is that passByReference is used (called)\u00a0the same way as passByValue:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">passByReference(betty)<\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">passByValue(betty)<\/code><\/p>\n<p>A\u00a0passByAddress requires that you specify an address or a pointer as the argument. That is, you need to send a pointer\/address:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int myVar = 5; \r\nint *myPointer = myVar; \/\/Pointer points to address of myVar\r\npassByPointer(myPointer)<\/pre>\n<p>or you need to put an ampersand (&amp;) in front of your variable to denote it&#8217;s address rather than it&#8217;s value:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">passByAddress(&amp;myVar)<\/code><\/p>\n<h4><strong>About the &amp; operator<\/strong><\/h4>\n<p>&#8220;&amp;&#8221; is for the\u00a0address of something. <b>But<\/b> when used when declaring a new variable, it becomes a different thing.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int a; <\/code><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">&amp;a;<\/code> \/\/ address of a<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int &amp;a = b;<\/code> \/\/\u00a0the &#8220;&amp;&#8221; now means you&#8217;re declaring a reference to &#8220;b&#8221;.<\/p>\n<p>Of course, when declaring a refence, you must immediately assign it to something. References must always refer to something, they can&#8217;t refer to nothing.<\/p>\n<p>So&#8230;<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int &amp;a;<\/code> \/\/ <b>not<\/b> valid<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int &amp;a = b;<\/code> \/\/\u00a0valid<\/p>\n<p>To use it with function, you need to declare the function this way, to accept references:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">void function(int &amp;a);<\/code> \/\/ takes a reference to an int<\/p>\n<p>And then you just send any &#8220;int&#8221; to it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int a;\r\nfunction(a);<\/pre>\n<p>It <b>looks<\/b> <b>like<\/b> we&#8217;re sending the value of &#8220;a&#8221;, but because the function was declared to accept references, not values,\u00a0you&#8217;re actually sending a reference to it.<\/p>\n<p>&#8212;&#8212;<\/p>\n<p>I also found some help from this <a href=\"http:\/\/stackoverflow.com\/questions\/3796181\/c-passing-pointer-to-function-howto-c-pointer-manipulation\">StackOverflow<\/a> thread:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">void makePointerEqualSomething(int *pInteger)\r\n{\r\n *pInteger = 7;\r\n}<\/pre>\n<p>In the function <strong>declaration<\/strong>, * means you are passing a pointer, but in its actual <strong>code body<\/strong>\u00a0* means you are accessing what the pointer is pointing at.<\/p>\n<p>In the function declaration, * means you are passing a pointer, but in its actual code body * means you are accessing what the pointer is pointing at.<\/p>\n<p>&amp; means get the address of something, its exact location in the computers memory, so <span style=\"font-family: monospace;\"><span style=\"font-size: 13px; line-height: 20.15px; background-color: #f9f9f9;\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int &amp;myVariable;<\/code>\u00a0<\/span><\/span><span style=\"line-height: 1.6;\">i<\/span><span style=\"line-height: 1.6;\">n a <\/span><strong style=\"line-height: 1.6;\">declaration<\/strong><span style=\"line-height: 1.6;\"> means the <strong>address<\/strong> of an integer or a pointer, while<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">int *pInteger;\r\nint someData;\r\npInteger = &amp;someData;<\/pre>\n<p>means make the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">pInteger<\/code>\u00a0pointer equal to the <strong>address<\/strong> of &#8216;someData&#8217; &#8211; so now <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">pInteger<\/code>\u00a0points at <code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">someData<\/code> and can be used to access it when you deference it:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">*pInteger = 9000;<\/code><\/p>\n<p>You can read that last line as &#8220;what is at the memory address pInteger\u00a0points to is equal to 9000.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last Saturday I had my final exam for my CS161 course. I did\u00a0okay, but I could have done better. I realize now that I really don&#8217;t have a handle on how pointers work still, so the goal of this post is to try and help myself understand them a little bit more. I was watching [&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-360","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\/360","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=360"}],"version-history":[{"count":10,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions\/377"}],"wp:attachment":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}