{"id":2890,"date":"2018-01-08T00:01:28","date_gmt":"2018-01-08T08:01:28","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2890"},"modified":"2018-01-05T20:37:29","modified_gmt":"2018-01-06T04:37:29","slug":"swapping-strings-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2890","title":{"rendered":"Swapping Strings &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2879\">Exercise<\/a> involves swapping elements between two string arrays. A number of solutions exist (as always), but because the &#8220;strings&#8221; are really pointers, the solution can be very specific. Yes, you just might have to use that dratted <code>**<\/code> notation. Brace yourselves.<br \/>\n<!--more--><br \/>\n<code>{yourselves}<\/code><\/p>\n<p>Ha! C humor . . .<\/p>\n<p>In the <em>main()<\/em> function, presented in the Exercise&#8217;s post, both functions <em>show_arrays()<\/em> and <em>swap_arrays()<\/em> are shown as the following statements:<\/p>\n<p><code>show_arrays(beatles,stones);<br \/>\nswap_arrays(beatles,stones);<\/code><\/p>\n<p>The pointer arrays are passed name-only. Because they&#8217;re pointer arrays, the function prototypes use the <code>**<\/code> notation:<\/p>\n<p><code>void show_arrays(char **b, char **s)<\/code><\/p>\n<p>and<\/p>\n<p><code>void swap_arrays(char **b, char **s)<\/code><\/p>\n<p>If the functions swallowed strings, you&#8217;d use <code>*b<\/code> and <code>*s<\/code> as arguments. But an array of strings, or an array of pointers, requires the <code>**<\/code> notation. <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=467\">Click here<\/a> to read my Lesson on pointer-pointers, if you need a review. <\/p>\n<p>The nifty thing, for those of you who detest pointers, is that once you&#8217;re inside the functions, you can again use array notation to show or swap the string elements. Here&#8217;s my solution for the <em>show_arrays()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nvoid show_arrays(char **b, char **s)\r\n{\r\n\tint x;\r\n\r\n\tprintf(\"Beatles =\");\r\n\tfor(x=0;x&lt;4;x++)\r\n\t\tprintf(\" %s\",b[x]);\r\n\tputchar('\\n');\r\n\tprintf(\"Stones =\");\r\n\tfor(x=0;x&lt;4;x++)\r\n\t\tprintf(\" %s\",s[x]);\r\n\tputchar('\\n');\r\n}<\/pre>\n<p>The <em>printf()<\/em> statements use <code>b[x]<\/code> and <code>s[x]<\/code> to display the strings. I could have written the expression in pointer notation, which would be <code>*(b+x)<\/code> and <code>*(s+x)<\/code>, respectively, but array notation is easier to read.<\/p>\n<p>Here is my solution for the <em>swap_arrays()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nvoid swap_arrays(char **b, char **s)\r\n{\r\n\tint x;\r\n\tchar *temp;\r\n\r\n\tfor(x=0;x&lt;4;x++)\r\n\t{\r\n\t\ttemp = b[x];\r\n\t\tb[x] = s[x];\r\n\t\ts[x] = temp;\r\n\t}\r\n}<\/pre>\n<p>Again, I use array notation within the function, which makes the solution look very similar to the solution for last month&#8217;s integer array-swapping function. The <code>*temp<\/code> variable is required to swap the pointers. It doesn&#8217;t need to be <code>**temp<\/code> because it&#8217;s referencing a string and not the string array.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/01\/01exercise.c\">Click here<\/a> to view my solution.<\/p>\n<p>When you swap pointers, you don&#8217;t affect the memory the pointers reference; the strings in the code continue to dwell in memory, safe and unmolested. The pointers, however, are swapped freely.<\/p>\n<p>If you attempted a solution that involved swapping the string elements, well, wow! And if it works, congratulations, but it was a lot more overhead than is required so solve the Exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise involves swapping elements between two string arrays. A number of solutions exist (as always), but because the &#8220;strings&#8221; are really pointers, the solution can be very specific. Yes, you just might have to use that dratted ** &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2890\">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":[5],"tags":[],"class_list":["post-2890","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2890","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=2890"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2890\/revisions"}],"predecessor-version":[{"id":2917,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2890\/revisions\/2917"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}