{"id":558,"date":"2014-02-22T00:01:13","date_gmt":"2014-02-22T08:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=558"},"modified":"2014-02-15T09:19:02","modified_gmt":"2014-02-15T17:19:02","slug":"how-the-pointer-variable-works-best","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=558","title":{"rendered":"How the ** Pointer Variable Works Best"},"content":{"rendered":"<p>The pointer-to-a-pointer variable does have a purpose, but fortunately it&#8217;s a very specific and rare purpose. And, no, that purpose isn&#8217;t to make you wish you had learned to program in Java.<br \/>\n<!--more--><br \/>\nFor review, here is the code concocted for <a href=\" http:\/\/c-for-dummies.com\/blog\/?p=467\">last week&#8217;s Lesson<\/a>, the full code, which includes the final line:<\/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    printf(\"**ptr2ptr is %d\\n\",**ptr2ptr);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample output on my PC looks like this:<\/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\r\nptr2ptr is 0028FF14\r\n*ptr2ptr is 0028FF18\r\n**ptr2ptr is 4<\/code><\/pre>\n<p>If you think this is a stupid way to get back to the value of variable <code>i<\/code>, you&#8217;re correct!<\/p>\n<p>No, you didn&#8217;t just waste a lot of time.<\/p>\n<p>The pointer-to-a-pointer variable isn&#8217;t used to reference a single pointer. And thank goodness! Instead, it&#8217;s used to reference an <em>array<\/em> of pointers. That&#8217;s how it&#8217;s best put to use. I suppose exceptions to this rule might exist, but I can&#8217;t think of any.<\/p>\n<blockquote><p>When you see a <code>**ptr<\/code> type of variable, it almost always refers to an array of pointers.<\/p><\/blockquote>\n<p>Here&#8217;s a quick review based on the above code and the stuff I preached in last week&#8217;s Lesson:<\/p>\n<p><code>*ptr<\/code> is a pointer variable, one that holds the address of a specific type of variable, such as an <em>int<\/em>, <em>char<\/em>, <em>float<\/em>, and so on.<\/p>\n<p><code>**ptr<\/code> is a pointer-to-a-pointer variable. It can hold the address of another pointer variable. The variable types still have to match, <em>int<\/em> for <em>int<\/em>, <em>char<\/em> for <em>char<\/em>, and so on.<\/p>\n<p>The way the <code>**ptr<\/code> construction is useful is when its value needs to be manipulated. That&#8217;s true for all variables, of course. After all, <code>**ptr<\/code> is a <em>variable<\/em>, a word which shares its root in the term <em>vary<\/em>. It makes no sense to create a <code>**ptr<\/code> type of variable unless its value is going to be manipulated.<\/p>\n<p>The <code>**ptr<\/code> variable is most useful when it references an array of pointers. The best example I can think of to demonstrate that use is to reference and manipulate an array of strings.<\/p>\n<p>Consider the months array, introduced in an earlier <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=441\">Lesson<\/a>:<\/p>\n<pre>code>char *months[] = {\r\n    \"January\", \"February\", \"March\", \"April\",\r\n    \"May\", \"June\", \"July\", \"August\",\r\n    \"September\", \"October\", \"November\", \"December\" };<\/code><\/pre>\n<p>This array isn&#8217;t an array of strings; the strings are created somewhere in memory. The array consists of the starting location for each string &#8212; a pointer. Here is Figure 1 from February 8th&#8217;s lesson, which shows those memory locations as they appear when dumped in Code::Blocks:<\/p>\n<div id=\"attachment_463\" style=\"width: 506px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-463\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/1221-figure1.png\" alt=\"Figure 1. How the months array looks in memory.\" width=\"496\" height=\"290\" class=\"size-full wp-image-463\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/1221-figure1.png 496w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/1221-figure1-300x175.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><p id=\"caption-attachment-463\" class=\"wp-caption-text\">Figure 1. How the <code>months<\/code> array looks in memory.<\/p><\/div>\n<p>To manipulate items in that array, you use a pointer-to-a-pointer variable. The example below demonstrates how <code>**m<\/code> can be used to manipulate the array:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *months[] = {\r\n        \"January\", \"February\", \"March\", \"April\",\r\n        \"May\", \"June\", \"July\", \"August\",\r\n        \"September\", \"October\", \"November\", \"December\" };\r\n    int x;\r\n    char **m;\r\n\r\n    m = months;\r\n    for(x=0;x&lt;12;x++)\r\n        printf(\"%s\\n\",*(m+x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This example is similar to the final example from the Lesson on February 8th. It shows how a pointer array is manipulated by using a <code>**<\/code> variable type. Even then, you can get into lots of trouble if you&#8217;re not careful.<\/p>\n<p>The pointer-to-a-pointer variable is pretty much on the far fringe of what you do with pointers in C. It&#8217;s the harbinger of the Segmentation Fault. Fortunately, beyond code such as what&#8217;s shown above,  the <code>**<\/code> construction isn&#8217;t something you deal with often in the C language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The story of the dratted pointer-to-a-pointer variable is strange, but one that does have a happy ending. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=558\">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-558","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\/558","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=558"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/558\/revisions"}],"predecessor-version":[{"id":579,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/558\/revisions\/579"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}