{"id":4929,"date":"2021-08-21T00:01:07","date_gmt":"2021-08-21T07:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4929"},"modified":"2021-08-28T08:32:00","modified_gmt":"2021-08-28T15:32:00","slug":"passing-strings-to-a-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4929","title":{"rendered":"Passing Strings to a Function"},"content":{"rendered":"<p>Despite teaching the C language, I still find myself at odds with pointers. Specifically, it&#8217;s the double pointers that remind me of my mortality. I found myself getting into the double pointer polka recently when I tried to work with an array of strings, passing each one individually to a function. O! The pain!<br \/>\n<!--more--><br \/>\nAs it turns out, my frustration was misplaced. Still, I&#8217;d like to impart my journey of woe and teach a bit more about double pointers.<\/p>\n<p>My desire is to process a series of strings in a function. Each string is stored in an array, which makes it a pointer-pointer thing. Anyhoo. The first step is to write a smaller version of the program that passes a single string without manipulating that string. To keep things simple, I use array notation:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_08_21-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_08_21-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid output(char a[])\r\n{\r\n    int x = 0;\r\n\r\n    while( a[x] )\r\n    {\r\n        putchar( a[x]);\r\n        x++;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\nint main()\r\n{\r\n    char string[] = \"Hello, world!\";\r\n\r\n    output(string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 17 declares <code>string[]<\/code> and assigns it text. This string is passed directly to the <em>output()<\/em> function at Line 19.<\/p>\n<p>The <em>output() <\/em>function specifies array notation in its argument list. This notation is used throughout the function, which should be easy to understand for all C programmers.<\/p>\n<p>Here is the same code, but using pointers:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_08_21-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_08_21-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid output(char *a)\r\n{\r\n    int x = 0;\r\n\r\n    while( *(a+x) )\r\n    {\r\n        putchar(*(a+x));\r\n        x++;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\nint main()\r\n{\r\n    char string[] = \"Hello, world!\";\r\n\r\n    output(string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>main()<\/em> function is the same for both versions of the program; you can pass a pointer to a string or a <em>char<\/em> array to the function. My focus was on the function &mdash; which might have been my first mistake.<\/p>\n<p>The <em>output()<\/em> function&#8217;s argument is a pointer, <code>char *a<\/code>. In the function, I&#8217;ve translated array notation into pointer notation: <code>a[x]<\/code> is similar to <code>*(a+x)<\/code>, a topic I cover in my books. Otherwise the two versions of the function operate the same and generate identical output.<\/p>\n<p>After these programs worked, my next step is to update the code so that multiple strings are processed from an array. The <em>output()<\/em> function need not change, so the following update reflects updates in the <em>main()<\/em> function:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_08_21-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2021_08_21-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid output(char *a)\r\n{\r\n    int x = 0;\r\n\r\n    while( *(a+x) )\r\n    {\r\n        putchar(*(a+x));\r\n        x++;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\nint main()\r\n{\r\n    char *string[3] = {\r\n        \"Hello, world!\",\r\n        \"This is a string test\",\r\n        \"Once upon a time, there was a string\"\r\n    };\r\n    int x;\r\n\r\n    for( x=0; x&lt;3; x++ )\r\n    {\r\n        output(string[x]);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this code, three strings are declared at Line 17. Technically, what&#8217;s declared is an array of <em>char<\/em> pointers, each of which references a string stored in memory. This note is important to remember, something that I eventually forgot.<\/p>\n<p>A <em>for<\/em> loop passes each string to the <em>output()<\/em> function, which uses pointer notation as shown earlier. Everything works; the three strings are output properly.<\/p>\n<p>At this point, I proved my code worked. But my goal is to modify the strings. This update is where I encountered my frustration with pointers. An array of strings <code>*string[]<\/code> is really <code>**string<\/code>. This relationship plays out in how a function can access and manipulate characters in the strings. Do things wrong and . . . <em>BOOM<\/em>!<\/p>\n<p>You might be able to see my problem right away. I can now, and it&#8217;s painful to me. Still, in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4933\">next week&#8217;s Lesson<\/a>, I explore how the manipulation process works in an updated version of the code, and how I screwed up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Passing strings to functions the easy way &mdash; but don&#8217;t count in it being easy for long. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4929\">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-4929","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\/4929","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=4929"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4929\/revisions"}],"predecessor-version":[{"id":4955,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4929\/revisions\/4955"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}