{"id":6195,"date":"2024-01-08T00:01:01","date_gmt":"2024-01-08T08:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6195"},"modified":"2023-12-30T12:04:53","modified_gmt":"2023-12-30T20:04:53","slug":"the-swap-function-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6195","title":{"rendered":"The <em>swap()<\/em> Function &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6180\">month&#8217;s Exercise<\/a> is to code a function that swaps two variables. The challenge really isn&#8217;t to swap the variables, but to figure out how to code a function that does so.<br \/>\n<!--more--><br \/>\nFunctions in C return only one value. The two tricks for getting around this limitation are to use a structure or pointers.<\/p>\n<p>Using a structure wasn&#8217;t part of the original challenge code, though I suppose you could have added a structure to define two integer members and swap them within the <em>swap()<\/em> function. A better solution, however, is to use pointers, which is what I did:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_01-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid swap(int *a, int *b)\r\n{\r\n    int *temp = a;\r\n    a = b;\r\n    b = temp;\r\n}\r\n\r\nint main()\r\n{\r\n    int x = 5;\r\n    int y = 10;\r\n\r\n    printf(\"Before swap: x=%d, y=%d\\n\",x,y);\r\n    swap(&amp;x, &amp;y);\r\n    printf(\"After swap: x=%d, y=%d\\n\",x,y);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>swap()<\/em> function accepts two integer pointers as its arguments, <code>a<\/code> and <code>b<\/code>. These are passed from the <em>main()<\/em> function by address: The <code>&<\/code> operator obtains the address in the <em>swap()<\/em> function call.<\/p>\n<p>In the <em>swap()<\/em> function, a third variable <code>temp<\/code> is created. It&#8217;s a pointer holding an address. It&#8217;s used to swap the two values, the memory locations, stored in passed variables <code>a<\/code> and <code>b<\/code>.<\/p>\n<p>Nothing is returned from the function as the variables&#8217; addresses are swapped. The values aren&#8217;t really affected.<\/p>\n<p>Of course, you could have coded the <em>swap()<\/em> function to swap the values and not the addresses. Here&#8217;s how this variation looks:<\/p>\n<pre class=\"screen\">\r\nvoid swap(int *a, int *b)\r\n{\r\n    int temp = *a;\r\n    *a = *b;\r\n    *b = temp;\r\n}<\/pre>\n<p>Variable <code>temp<\/code> is an integer. It snatches the value stored at the address in <code>a<\/code>, then swap works with dereferenced pointers. The output is the same.<\/p>\n<p>Either solution is possible. You may have come up with another solution as well. If so, and if the output looks like this:<\/p>\n<p><code>Before swap: x=5, y=10<br \/>\nAfter swap: x=10, y=5<\/code><\/p>\n<p>You&#8217;ve passed the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is to code a function that swaps two variables. The challenge really isn&#8217;t to swap the variables, but to figure out how to code a function that does so.<\/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-6195","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\/6195","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=6195"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6195\/revisions"}],"predecessor-version":[{"id":6199,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6195\/revisions\/6199"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}