{"id":467,"date":"2014-02-15T00:01:58","date_gmt":"2014-02-15T08:01:58","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=467"},"modified":"2014-05-06T08:09:11","modified_gmt":"2014-05-06T15:09:11","slug":"the-ongoing-mystery-of-the-variable","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=467","title":{"rendered":"The Ongoing Mystery of the ** Variable"},"content":{"rendered":"<p>The <code>**<\/code> notation can really make a beginning C programmer blanch. In fact, a lot of Java programmers probably fled from the C language just after the initial lesson on pointers. They&#8217;d turn to stone even to glance at a variable like <code>**blorf<\/code>. You don&#8217;t need to cower under a desk with those people.<br \/>\n<!--more--><br \/>\nSuppose an <em>int<\/em> pointer is declared as <code>ptr<\/code>:<\/p>\n<p><code>int *ptr;<\/code><\/p>\n<p>To use the pointer you have to initialize it, storing the address of some other <em>int<\/em> variable in the pointer, because:<\/p>\n<blockquote><p>A pointer is a variable that holds a memory location. Specifically it holds the address of another variable.<\/p><\/blockquote>\n<p>For example, variable <code>i<\/code>:<\/p>\n<p><code>ptr = &i;<\/code><\/p>\n<p>After that declaration in the code, pointer variable <code>ptr<\/code> holds the address of variable <code>i<\/code>. This process should be familiar to you if you read through one of my C programming books. Here&#8217;s some code you can chew on for review:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int i;\r\n    int *ptr;\r\n\r\n    i = 4;      \/* initialize i *\/\r\n    ptr = &i;   \/* initialize ptr *\/\r\n\r\n    printf(\"i is %d\\n\",i);\r\n    printf(\"&i is %p\\n\",&i);\r\n    printf(\"*ptr is %d\\n\",*ptr);\r\n    printf(\"ptr is %p\\n\",ptr);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code shows how the pointer and variable are related. Here&#8217;s sample output:<\/p>\n<pre><code>i is 4\r\n&i is 0028FF18\r\n*ptr is 4\r\nptr is 0028FF18<\/code><\/pre>\n<p>Figure 1 shows the Watches window in Code::Blocks, which further demonstrates the equivalency between the variables. Pay attention to the far right column, which lists the variable types. See how both <code>&i<\/code> and <code>ptr<\/code> are basically the same type, <code>int *<\/code>, which means an integer pointer, or the memory location of an integer value.<\/p>\n<div id=\"attachment_553\" style=\"width: 506px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-553\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure1.png\" alt=\"Figure 1. The Watches window shows how the variable values and types are equivalent.\" width=\"496\" height=\"184\" class=\"size-full wp-image-553\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure1.png 496w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure1-300x111.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><p id=\"caption-attachment-553\" class=\"wp-caption-text\">Figure 1. The Watches window shows how the variable values and types are equivalent.<\/p><\/div>\n<p>Just for giggles, Figure 2 illustrates the bytes stored at memory location <code>0x28ff18<\/code>. Integer values on my PC are stored in 32 bits, which is 4 bytes wide. You can see the value 4 stored in those four bytes, highlighted in the figure. (The value is stored backwards in memory, which is good for the processor, but not for humans reading a data dump.)<\/p>\n<div id=\"attachment_554\" style=\"width: 609px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-554\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure2.png\" alt=\"Figure 2. The value of variable i as stored in memory.\" width=\"599\" height=\"245\" class=\"size-full wp-image-554\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure2.png 599w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure2-300x122.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/02\/0215-figure2-500x204.png 500w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><p id=\"caption-attachment-554\" class=\"wp-caption-text\">Figure 2. The value of variable <code>i<\/code> as stored in memory.<\/p><\/div>\n<p>As with all variables, a pointer variable has a location in memory. To obtain that location, the unary <code>&<\/code> operator is used. The end result, the address of a pointer variable, is effectively a pointer-to-a-pointer.<\/p>\n<p><em>Don&#8217;t let that sink in; just keep reading!<\/em><\/p>\n<p>The address of a pointer variable is where those weird <code>**<\/code> things come from. That&#8217;s because a variable with two unary <code>*<\/code> operators is a pointer-to-a-pointer.<\/p>\n<p>Consider the following code, which adds a pointer-to-a-pointer variable, <code>ptr2ptr<\/code>, to the original code listed earlier:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int i;\r\n    int *ptr;\r\n    int **ptr2ptr;\r\n\r\n    i = 4;\r\n    ptr = &i;\r\n                \/* initialize ptr2ptr: *\/\r\n    ptr2ptr = &ptr;\r\n\r\n    printf(\"i is %d\\n\",i);\r\n    printf(\"&i is %p\\n\",&i);\r\n    printf(\"*ptr is %d\\n\",*ptr);\r\n    printf(\"ptr is %p\\n\",ptr);\r\n                \/* new stuff here: *\/\r\n    putchar('\\n');\r\n    printf(\"&ptr is %p\\n\",&ptr);\r\n    printf(\"ptr2ptr is %p\\n\",ptr2ptr);\r\n    printf(\"*ptr2ptr is %p\\n\",*ptr2ptr);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>ptr2ptr<\/code> variable is initialized at Line 12. It&#8217;s assigned the address of the <code>ptr<\/code> pointer. A <code>**<\/code> variable like <code>ptr2ptr<\/code> holds the address of a pointer. In this code that pointer is an <em>int<\/em> pointer, <code>*ptr<\/code>.<\/p>\n<p>Here&#8217;s the program output, along with my comments (in red) explaining the last three lines:<\/p>\n<pre><code>i is 4\r\n&i is 0028FF18\r\n*ptr is 4\r\nptr is 0028FF18\r\n\r\n&ptr is 0028FF14      <span style=\"color:red\">The address of variable ptr in memory<\/span>\r\nptr2ptr is 0028FF14   <span style=\"color:red\">Also the address of variable ptr in memory<\/span>\r\n*ptr2ptr is 0028FF18  <span style=\"color:red\">The address of variable i in memory<\/span><\/code><\/pre>\n<p>The memory address of <code>ptr<\/code> is fetched by using the <code>&<\/code> operator. In the above output, that location is <code>0x28FF14<\/code>.<\/p>\n<p>Variable <code>ptr2ptr<\/code> stores the address of variable <code>ptr<\/code>. You can see that by the second to last line of output. Both lines display <code>0x28FF14<\/code>.<\/p>\n<p>At the last line, the contents of variable <code>ptr2ptr<\/code> when used with the unary <code>*<\/code> operator are the same as the contents of the <code>ptr<\/code> variable, the address of variable <code>i<\/code>.<\/p>\n<p>The final step is to add the following line to the code, just after the last <em>printf()<\/em> statement above the <em>return<\/em> statement:<\/p>\n<pre><code>printf(\"**ptr2ptr is %d\\n\",**ptr2ptr);<\/code><\/pre>\n<p>Compile and run on your own. Doing so will help you better understand what&#8217;s going on, or at least take you one step closer. I&#8217;ll explain more in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=558\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I continue my exploration of pointers-to-pointers by attempting to clear up how a <em>**ptr<\/em> thing can be used in your code. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=467\">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-467","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\/467","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=467"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/467\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/467\/revisions\/715"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}