{"id":3157,"date":"2018-06-23T00:01:53","date_gmt":"2018-06-23T07:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3157"},"modified":"2018-06-16T13:55:40","modified_gmt":"2018-06-16T20:55:40","slug":"a-weird-thing-ive-never-seen-before","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3157","title":{"rendered":"A Weird Thing I&#8217;ve Never Seen Before"},"content":{"rendered":"<p>While browsing some sample code recently, I encountered the following C language expression:<\/p>\n<p><code>x = &(*ptr);<\/code><\/p>\n<p>Both variables <code>x<\/code> and <code>ptr<\/code> are pointers. So what the heck is going on?<br \/>\n<!--more--><br \/>\nOften I copy a sample program from the Internet to see what it does. Because I typically don&#8217;t like the way the coder presents things (often because not everyone really understands pointers), I work with the code, modify it, and eventually concoct my own version.<\/p>\n<p>In this instance, I dutifully copied the code, which dealt with reading data from a PNG image file. From the sample line above, the <code>ptr<\/code> variable references an array of pointers. So it can be declared as:<\/p>\n<p><code>**ptr<\/code><\/p>\n<p>Or:<\/p>\n<p><code>*ptr[]<\/code><\/p>\n<p>I use the latter format in my code, though I may change it to the <code>**<\/code> format after I fully grasp what&#8217;s going on. In this case, the variable <code>x<\/code> (in my example) is pulling an address from one of the pointers (memory locations) stored in the <code>ptr<\/code> array. But what the heck is <code>&(*ptr)<\/code>? It looks terribly confusing.<\/p>\n<p>My assumption was that the <code>&<\/code> and <code>*<\/code> operators canceled each other out, but I wasn&#8217;t sure. Though I enjoy using pointers and try to fully understand them, this notation befuddled me. So, I wrote this code to help unravel the mystery:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char z,*p;\r\n\r\n    z = 'A';\r\n    p = &amp;z;\r\n\r\n    printf(\"%p == %p\\n\",p,&amp;z);\r\n    printf(\"%c == %c\\n\",*p,z);\r\n    printf(\"&amp;(*p) = %p\\n\",&amp;(*p));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>It&#8217;s best to explain what&#8217;s going on by viewing the output:<\/p>\n<pre><code>0x7ffee6841a6b == 0x7ffee6841a6b\r\nA == A\r\n&(*p) = 0x7ffee6841a6b<\/code><\/pre>\n<p>The first line compares the values of pointer variable <code>p<\/code>, which is assigned to the address of variable <code>z<\/code>, with the address of variable <code>z<\/code> directly. The values should be equal, and they are.<\/p>\n<p>The second line compares the contents of each variable, first <code>z<\/code> directly and then <code>z<\/code> as referenced by variable <code>p<\/code>. Both are identical.<\/p>\n<p>Finally, I perform the weirdo <code>&(*p)<\/code> operation on variable <code>p<\/code>. The result is the address of variable <code>z<\/code>, which is the original contents of variable <code>p<\/code>.<\/p>\n<p>Because of these results, I confirmed my hypothesis that the <code>&<\/code> and the <code>*<\/code> operators cancel each other out.<\/p>\n<p>Back in my PNG-reading program I edited the original statement to remove the redundancies.<\/p>\n<p>The only puzzle that remains is how someone would think to use <code>&(*p)<\/code> as an operation in the first place? It could have been the result of a search-and-replace in the code. It could have been bad documentation at some point. Or, most likely, it could be yet another C programmer who doesn&#8217;t fully understand pointers and who piled on the operators and was fortunate enough to get the result he needed.<\/p>\n<p>Pointers are mysterious and troubling to many coders. Yet apparently, some are bold enough to try this or that and publish the success on the Internet without ever fully understanding what&#8217;s going on. I hope you reach the point in your C programming career where you don&#8217;t have to do that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rare is it that I encounter C code I&#8217;m unfamiliar with, but it happens often enough. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3157\">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-3157","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\/3157","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=3157"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3157\/revisions"}],"predecessor-version":[{"id":3164,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3157\/revisions\/3164"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}