{"id":2310,"date":"2017-01-28T00:01:13","date_gmt":"2017-01-28T08:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2310"},"modified":"2017-02-04T07:48:40","modified_gmt":"2017-02-04T15:48:40","slug":"manipulate-pointers-in-functions-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2310","title":{"rendered":"Manipulate Pointers in Functions, Part III"},"content":{"rendered":"<p>I know C programmers who make it their goal to avoid pointers &mdash; even where necessary. It&#8217;s possible to do so, and many have cheat sheets to help them. Then comes the <code>**<\/code> pointer monster, which can be avoided altogether . . . until you need to manipulate a pointer variable&#8217;s own address within a function. Then you&#8217;re cursed.<br \/>\n<!--more--><br \/>\nLike every variable type, a pointer has an address in memory, a location. So when you create <em>char<\/em> pointer <code>*text<\/code>, the program requests that a pointer-sized chunk of memory be set aside. When that pointer is initialized, the address of the variable it references is stored in its chunk-o-memory.<\/p>\n<p>From the sample program from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2306\">last week&#8217;s Lesson<\/a>, variable <code>*text<\/code> is inialized to a string buffer in the <em>main()<\/em> function:<\/p>\n<p><code>char *text = \"ABCD\\n\";<\/code><\/p>\n<p>When variable <code>text<\/code> is used, it refers to the starting location, the memory address, where <code>\"ABCD\\n\"<\/code> is located.<\/p>\n<p>When variable <code>*text<\/code> is used, it references the character (because <code>text<\/code> is a <em>char<\/em> pointer) at the memory address stored in <code>text<\/code>, which would be the character <code>'A'<\/code>. Figure 1 illustrates this concept.<\/p>\n<div id=\"attachment_2322\" style=\"width: 437px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2322\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure1.png\" alt=\"\" width=\"427\" height=\"482\" class=\"size-full wp-image-2322\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure1.png 427w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure1-266x300.png 266w\" sizes=\"auto, (max-width: 427px) 100vw, 427px\" \/><p id=\"caption-attachment-2322\" class=\"wp-caption-text\">Figure 1. The pointer variable <code>text<\/code> contains an address; variable <code>*text<\/code> references the contents of that address.<\/p><\/div>\n<p>To manipulate the contents of the pointer&#8217;s address in a function, you just pass the pointer. That&#8217;s what I showed in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2288\">Part I of this series<\/a>. But I also demonstrated that you can&#8217;t manipulate the pointer&#8217;s address; it&#8217;s unaffected because the function doesn&#8217;t own the pointer variable.<\/p>\n<p>When you need to manipulate a pointer&#8217;s address in a function, pass a pointer to the pointer.<\/p>\n<p>Bear with me.<\/p>\n<p>You start by getting the address of the pointer variable: Use the <code>&<\/code> operator just as you would on any other variable. Figure 2 illustrates.<\/p>\n<div id=\"attachment_2323\" style=\"width: 392px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2323\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure2.png\" alt=\"\" width=\"382\" height=\"206\" class=\"size-full wp-image-2323\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure2.png 382w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure2-300x162.png 300w\" sizes=\"auto, (max-width: 382px) 100vw, 382px\" \/><p id=\"caption-attachment-2323\" class=\"wp-caption-text\">Figure 2. The memory location of variable <code>text<\/code>, which holds the address of string <code>\"ABCD\\n\"<\/code>.<\/p><\/div>\n<p>In Figure 2, you see the address of pointer <code>text<\/code>, which is shown as value <code>0x7FE88<\/code>. The contents of that location are the address of the string <code>\"ABCD\\n\"<\/code>, or <code>0x7FF00<\/code>, which is illustrated in Figure 1.<\/p>\n<p>The pointer&#8217;s address by itself is a curiosity, but the value can be saved in a pointer-pointer variable. The <em>char<\/em> variable <code>**p<\/code> is designed to hold the address of a pointer, or the address of (<code>*<\/code>) the address of (<code>*<\/code>) some variable.<\/p>\n<p>This is spot where programmers become overwhelmed, which is easy to do to. References to references are something that human brains don&#8217;t regularly deal with.<\/p>\n<p>To make the <code>&text<\/code> address value useful, last week&#8217;s program created the <code>**p<\/code> variable. It&#8217;s assigned to the value of <code>&text<\/code> when that value is passed to the <em>fourchar()<\/em> function. As a statement, the assignment looks like this:<\/p>\n<p><code>p = &text;<\/code><\/p>\n<p>Figure 3 illustrates all the gears and cogs of this machination.<\/p>\n<div id=\"attachment_2324\" style=\"width: 243px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2324\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure3.png\" alt=\"\" width=\"233\" height=\"512\" class=\"size-full wp-image-2324\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure3.png 233w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0128-figure3-137x300.png 137w\" sizes=\"auto, (max-width: 233px) 100vw, 233px\" \/><p id=\"caption-attachment-2324\" class=\"wp-caption-text\">Figure 3. How the <code>*text<\/code> and <code>**p<\/code> variables work together.<\/p><\/div>\n<p>The goal of all these words and illustrations is that if you can&#8217;t get your head wrapped around the pointer-pointer concept, at least you can look up a handy reference that explains how the various pieces work. That&#8217;s what I do in Figure 3, which is often what I write down while I&#8217;m programming so that I can use the proper references.<\/p>\n<p>And now the Big Reveal: The pointer and pointer-pointer relationship has a lot to do with two-dimensional arrays. I cover this topic in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2329\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pointers and pointers-to-pointers and addresses of pointers and all that stuff explained. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2310\">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-2310","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\/2310","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=2310"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2310\/revisions"}],"predecessor-version":[{"id":2364,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2310\/revisions\/2364"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}