{"id":2879,"date":"2018-01-01T00:01:11","date_gmt":"2018-01-01T08:01:11","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2879"},"modified":"2018-01-08T08:12:59","modified_gmt":"2018-01-08T16:12:59","slug":"swapping-strings","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2879","title":{"rendered":"Swapping Strings"},"content":{"rendered":"<p>Last month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2840\">Exercise<\/a> was rather silly: Rarely do you need to swap elements between two integer arrays. As Chris pointed out in the Exercise&#8217;s comments, &#8220;Would it not be easier to just use the array which contains the values you need?&#8221; Indeed, it is, which leads up to this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nConsider that you have two arrays:<\/p>\n<pre class=\"screen\">\r\nchar *beatles[4] = {\r\n    \"John\", \"Paul\", \"George\", \"Ringo\"\r\n};\r\nchar *stones[4] = {\r\n    \"Mick\", \"Keith\", \"Ronnie\", \"Charlie\"\r\n};<\/pre>\n<p>These are not string arrays, but pointer arrays. The declaration <code>char *<\/code> defines a pointer, and <code>char *beatles[4]<\/code> references four pointers. The pointers are all preset (in both arrays above) to various strings.<\/p>\n<blockquote><p>Indeed, when you see such a string declaration, you must be careful not to manipulate the strings directly. If you do, you experience all sorts of pointer woe, but that&#8217;s not the point of this Exercise.<\/p><\/blockquote>\n<p>Your task is to swap pointers between the two arrays. I would write, &#8220;swap strings between the two arrays&#8221; (and that&#8217;s this post&#8217;s title), but what you&#8217;re really doing is swapping pointers. In fact, if your solution involves manipulating the strings, you&#8217;ll soon discover the ugly truth &mdash; but I&#8217;ll leave that frustration to you.<\/p>\n<p>Here&#8217;s the rest of the <em>main()<\/em> function for this month&#8217;s Exercise:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    char *beatles[4] = {\r\n        \"John\", \"Paul\", \"George\", \"Ringo\"\r\n    };\r\n    char *stones[4] = {\r\n        \"Mick\", \"Keith\", \"Ronnie\", \"Charlie\"\r\n    };\r\n\r\n    printf(\"Original arrays:\\n\");\r\n    show_arrays(beatles,stones);\r\n\r\n    swap_arrays(beatles,stones);\r\n\r\n    printf(\"After swap:\\n\");\r\n    show_arrays(beatles,stones);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2890\">solution <\/a>is quite similar to last month&#8217;s Exercise; you write your own versions of the <em>show_arrays()<\/em> and <em>swap_arrays()<\/em> functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick method exists to swap string elements between two arrays. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2879\">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":[3],"tags":[],"class_list":["post-2879","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2879","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=2879"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2879\/revisions"}],"predecessor-version":[{"id":2935,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2879\/revisions\/2935"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}