{"id":2288,"date":"2017-01-14T00:01:21","date_gmt":"2017-01-14T08:01:21","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2288"},"modified":"2017-01-21T08:21:17","modified_gmt":"2017-01-21T16:21:17","slug":"manipulate-pointers-in-functions-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2288","title":{"rendered":"Manipulate Pointers in Functions, Part I"},"content":{"rendered":"<p>It&#8217;s common to pass a pointer to a function. Within the function, you can manipulate the data the pointer references without having to return that data from the function. This aspect of a pointer is what makes C a powerful &mdash; and scary &mdash; programming language. But what about when you need to manipulate the pointer&#8217;s address in the function?<br \/>\n<!--more--><br \/>\nWhile you can manipulate the data a pointer references, within a function you cannot manipulate the pointer itself (its address). That&#8217;s because the function doesn&#8217;t &#8220;own&#8221; the pointer variable; it uses a copy. Like any other variable passed to a function, the original variable is unchanged unless it&#8217;s a global (external) variable.<\/p>\n<p>Pointers are confusing enough without having to add another layer of complexity, but occasionally I run into these types of puzzles. You will, too, unless you forget how variables are passed to functions and your brilliant program doesn&#8217;t work.<\/p>\n<p>As is usually the case, it&#8217;s best to show sample code to illustrate the problem:<\/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(text);\r\n    putchar(*text);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>String <code>text<\/code> is declared as a pointer in the <em>main()<\/em> function. Its address (<code>text<\/code>) is passed to function <em>fourchar()<\/em>, which manipulates the pointer to display four characters in the string. In the function, each character is referenced by <code>*p<\/code> and pointer <code>p<\/code> is incremented.<\/p>\n<p>When <em>fourchar()<\/em> returns, the <em>putchar()<\/em> function displays the character <code>*text<\/code>. You might think that because <code>p<\/code> was incremented in the <em>fourchar()<\/em> function, that the value of <code>text<\/code> was also manipulated. If so, <code>*text<\/code> references the <code>'\\n'<\/code> character in the string. But it doesn&#8217;t, as the program&#8217;s sample output shows:<\/p>\n<pre><code>ABCDA<\/code><\/pre>\n<p>The problem isn&#8217;t that the pointer is called <code>p<\/code> in function <em>fourchar()<\/em> and <code>text<\/code> in <em>main()<\/em>; they are two different variables. No, the problem is that only the pointer&#8217;s address is passed to the function; the pointer variable itself remains intact and unchanged inside the <em>main()<\/em> function. This aspect holds true for all variables passed to functions, but it&#8217;s extra weird because you can still manipulate a pointer&#8217;s referenced data.<\/p>\n<p>Bottom line: When the <em>fourchar()<\/em> function returns, the <em>putchar()<\/em> statement displays the <code>'A'<\/code> in string <code>text<\/code>. That&#8217;s because the address in <code>text<\/code> hasn&#8217;t been changed within the <em>main()<\/em> function.<\/p>\n<p>Still, after all this explaining and understanding, I have a question: How can the code manipulate a pointer within a function and have that manipulation stick?<\/p>\n<p>For example, I want the <em>fourchar()<\/em> function to adjust the value of the <code>text<\/code> pointer and have that manipulation retained when the function call returns. Such a thing is possible, and I&#8217;ll explore that solution in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2306\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pointers passed to functions transform into local variables, which can frustrate your otherwise well-laid plans. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2288\">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-2288","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\/2288","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=2288"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2288\/revisions"}],"predecessor-version":[{"id":2336,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2288\/revisions\/2336"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}