{"id":2306,"date":"2017-01-21T00:01:12","date_gmt":"2017-01-21T08:01:12","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2306"},"modified":"2017-01-28T07:55:16","modified_gmt":"2017-01-28T15:55:16","slug":"manipulate-pointers-in-functions-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2306","title":{"rendered":"Manipulate Pointers in Functions, Part II"},"content":{"rendered":"<p>When a function must manipulate a pointer&#8217;s address, the argument passed is a pointer-pointer, not a pointer. Confused? Oh, I&#8217;m just getting started . . .<br \/>\n<!--more--><br \/>\nAs a review:<\/p>\n<ul>\n<li>A pointer is a variable that holds a memory address of another variable.<\/li>\n<li>The pointer variable can be used to manipulate the contents of the address.<\/li>\n<li>The pointer variable can be used to manipulate the address (the address value can be changed).<\/li>\n<\/ul>\n<p>Manipulating the contents of a pointer in a function is easy. To manipulate the pointer&#8217;s address, however, you must pass the pointer&#8217;s address, or a pointer-pointer, to the function.<\/p>\n<p>It&#8217;s true: Like every other variable type, pointers have addresses in memory.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2288\">last week&#8217;s Lesson<\/a>, the pointer <code>text<\/code> is passed to function <em>fourchar()<\/em>. Here&#8217;s the declaration of that function:<\/p>\n<p><code>void fourchar(char *p)<\/code><\/p>\n<p>And this statement calls the function from <em>main()<\/em>:<\/p>\n<p><code>fourchar(text);<\/code><\/p>\n<p>Within the function, you use pointer <code>p<\/code> to manipulate values at memory location <code>text<\/code>. You can change pointer <code>p<\/code> in the function, but the original pointer <code>text<\/code> dwells in the <em>main()<\/em> function unchanged &mdash; that is, unless you pass its address to the function. To do so, you use this statement in <em>main()<\/em>:<\/p>\n<p><code>fourchar(&text);<\/code><\/p>\n<p>The address of <code>text<\/code> can be manipulated in function <em>fourchar()<\/em>. The declaration of <em>fourchar()<\/em> must be changed to accept the new variable type:<\/p>\n<p><code>void fourchar(char **p)<\/code><\/p>\n<p>Yes, it&#8217;s the ugly <code>**<\/code> operator that drives everyone nuts, but that&#8217;s how you can modify a pointer&#8217;s address &mdash; as well as the contents at that address &mdash; within a function. Indeed, things get truly ugly in this operation, but they&#8217;re still workable.<\/p>\n<p>The following modification to last week&#8217;s code accepts a pointer-to-a-pointer as an argument to the <em>fourchar()<\/em> function, manipulates the pointer&#8217;s address directly, as well as displays the contents of the pointer-to-a-pointer address:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fourchar(char **p)\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;4;x++)\r\n    {\r\n        putchar(**p);\r\n        (*p)++;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char *text = \"ABCD\\n\";\r\n\r\n    fourchar(&amp;text);\r\n    putchar(*text);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Within function <em>fourchar()<\/em>, the character at <code>*text<\/code> is referenced as <code>**p<\/code>. This is where some programmers doze off, but pay attention! <code>**p<\/code> translates as, &#8220;The contents of the address stored at address <code>p<\/code>.&#8221;<\/p>\n<p>Within the <em>fourchar()<\/em> function, variable <code>*p<\/code> is a memory location, not a character. It holds the value passed to the function, the address of pointer <code>text<\/code>. To access that memory location&#8217;s value, what would be <code>*text<\/code> in the <em>main()<\/em> function, you use <code>**p<\/code> in function <em>fourchar()<\/em>.<\/p>\n<p>The next line manipulates the address stored in <code>*p<\/code> &mdash; the value of the <em>main()<\/em> function&#8217;s <code>text<\/code> pointer variable:<\/p>\n<p><code>(*p)++;<\/code><\/p>\n<p>In the <em>main()<\/em> function, you&#8217;d use <code>text++<\/code>, but because the address of <code>text<\/code> was passed to the <em>fourchar()<\/em> function, you need to use a pointer, <code>*p<\/code>. Further, because of the order of precedence, you must ensure that the pointer at <code>*p<\/code> is incremented, so you need parentheses: <code>(*p)++<\/code>.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>ABCD<\/code><\/pre>\n<p>Unlike last week&#8217;s code, the pointer&#8217;s address is changed within the <em>fourchar()<\/em> function. When the function returns, the address stored in <code>text<\/code> references the newline character, which is displayed in the output.<\/p>\n<p>I&#8217;ll explain the pointer-to-pointer relationship in further detail, along with friendly illustrations, in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2310\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t change a pointer&#8217;s address in a function, but you can change a pointer-to-a-pointer&#8217;s address. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2306\">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-2306","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\/2306","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=2306"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2306\/revisions"}],"predecessor-version":[{"id":2345,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2306\/revisions\/2345"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}