{"id":4938,"date":"2021-09-04T00:01:00","date_gmt":"2021-09-04T07:01:00","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4938"},"modified":"2021-08-28T12:04:51","modified_gmt":"2021-08-28T19:04:51","slug":"when-passing-a-string-to-a-function-be-careful-that-you-dont","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4938","title":{"rendered":"When Passing a String to a Function, Be Careful That You Don&#8217;t . . ."},"content":{"rendered":"<p>Passing a string to a function? Check!<br \/>\nPassing an array of strings to a function? Watch out!<br \/>\n<!--more--><br \/>\nFrom <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4933\">last week&#8217;s Lesson<\/a>, I&#8217;m attempting to pass an array of strings to a function, one at a time. Within the function, the string&#8217;s characters are modified. The modified string is output from the <em>main()<\/em> function.<\/p>\n<p>My original code blew up. The strings are passed to the function just fine, but once the string&#8217;s characters are modified, <em>BOOM<\/em>.<\/p>\n<p>I kept working and working the function, looking in a debugger, and getting cross with my cats. Nothing helped.<\/p>\n<p>Then I looked at the string definition:<\/p>\n<p><code>char *strings[3] = {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"Hello, world!\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"This is a string test\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"Once upon a time, there was a string\"<\/code><\/p>\n<p>May the gods forgive me.<\/p>\n<p>This declaration commits the sin of declaring a string as a pointer. You can do it, but lord help you if you attempt to modify the string. I&#8217;ve even <a href=\"http:\/\/a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3475\">written about this topic<\/a> before! How stupid can I be?<\/p>\n<p>You want to be extremely careful when you declare a string literal as a pointer: individually or in an array. If the strings aren&#8217;t to be modified, fine. Declare them as <em>const<\/em> types. No problem.<\/p>\n<p>If you choose to modify the strings, just don&#8217;t initialize the array of pointers. This way, you can pass the string pointer to a function and manipulate it. To avoid the mistake, the strings must be initialized elsewhere, then their addresses assigned to the <em>char<\/em> pointer array.<\/p>\n<p>For example, you start like this:<\/p>\n<p><code>char *strings[3];<\/code><\/p>\n<p>This array is declared as storage for three <em>char<\/em> pointers, which eventually will reference strings. Observe that this declaration doesn&#8217;t assign anything to the array; it&#8217;s just storage. The strings to manipulate, and eventually reference in the <code>strings[]<\/code> array, are declared on their own:<\/p>\n<p><code>char a[] = \"Hello, world!\";<br \/>\nchar b[] = \"This is a string test\";<br \/>\nchar c[] = \"Once upon a time, there was a string\";<\/code><\/p>\n<p>These strings are <em>char<\/em> arrays, not pointers. The array name serves as the base address for each string, like a pointer. In fact, the next step is to reference each array as an element within the <code>strings[]<\/code> pointer array:<\/p>\n<p><code>strings[0] = a;<br \/>\nstrings[1] = b;<br \/>\nstrings[2] = c;<\/code><\/p>\n<p>These statements copy the address of each string into the <code>string[]<\/code> array&#8217;s elements. That&#8217;s it. The <em>char<\/em> pointer array is now initialized properly. The strings referenced in the array can be sent to a function for direct manipulation, as this modification to my earlier code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_09_04-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_09_04-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid increase(char *a)\r\n{\r\n    while(*a)\r\n    {\r\n        *a = *a + 1;\r\n        a++;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char *strings[3];\r\n    char a[] = \"Hello, world!\";\r\n    char b[] = \"This is a string test\";\r\n    char c[] = \"Once upon a time, there was a string\";\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* allocate pointers *\/<\/span>\r\n    strings[0] = a;\r\n    strings[1] = b;\r\n    strings[2] = c;\r\n\r\n    <span class=\"comments\">\/* copy the strings *\/<\/span>\r\n\r\n    for( x=0; x&lt;3; x++ )\r\n    {\r\n        printf(\"%s = \",strings[x]);\r\n        increase(strings[x]);\r\n        printf(\"%s\\n\",strings[x]);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program now works, with no excess choking taking place in the <em>increase()<\/em> function:<\/p>\n<p><code>Hello, world! = Ifmmp-!xpsme\"<br \/>\nThis is a string test = Uijt!jt!b!tusjoh!uftu<br \/>\nOnce upon a time, there was a string = Podf!vqpo!b!ujnf-!uifsf!xbt!b!tusjoh<\/code><\/p>\n<p>What I&#8217;ve learned from this episode is, first, always remember those things I tell others never to forget: The direct pointer-string assignment is fine for referencing strings, but such strings cannot be modified!<\/p>\n<p>Second, I learned that if you do want to modify strings in an array, assign the string values separately. May I never forget that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Initializing a string as a pointer works, but . . . <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4938\">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-4938","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\/4938","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=4938"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4938\/revisions"}],"predecessor-version":[{"id":4970,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4938\/revisions\/4970"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}