{"id":5660,"date":"2022-12-08T00:01:46","date_gmt":"2022-12-08T08:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5660"},"modified":"2022-12-03T11:43:37","modified_gmt":"2022-12-03T19:43:37","slug":"quick-sorting-structures-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5660","title":{"rendered":"Quick Sorting Structures &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5650\">this month&#8217;s Exercise<\/a> is to extract a structure member from a <em>void<\/em> pointer for use in the quick-sort <em>compare()<\/em> function. This isn&#8217;t a solution you can readily find on the Interwebs.<br \/>\n<!--more--><br \/>\nI must have hammered away at various solutions for hours when originally confronted with this problem. I tried every possible combination of asterisk\/parentheses weirdo expression I could muster. Eventually, stepping away from the keyboard &mdash; and avoiding the temptation to drink heavily &mdash; yielded a clean solution.<\/p>\n<p>The first thing you must realize is that it&#8217;s weirdly complicated to construct a single <em>return<\/em> statement to handle the job. Rather than keep trying, I decided to first convert the <em>void<\/em> pointers into compatible structures. Here is the format:<\/p>\n<p><code>one = *(struct film *)a;<\/code><\/p>\n<p>Variable <code>one<\/code> is a <em>film<\/em> structure. It&#8217;s assigned to the passed pointer <code>*a<\/code>, first argument in the <em>compare()<\/em> function, which must be typecast to a <em>film<\/em> structure pointer. Once this conversion is made, <em>struct<\/em> variable <code>one<\/code> is available for comparing the <code>year<\/code> member values, as shown in my full solution here:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_12-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_12-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct film {\r\n    char title[32];\r\n    int year;\r\n};\r\n\r\nint compare(const void *a, const void *b)\r\n{\r\n    struct film one,two;\r\n\r\n    one = *(struct film *)a;\r\n    two = *(struct film *)b;\r\n\r\n    return( one.year - two.year );\r\n}\r\n\r\nint main()\r\n{\r\n    const int size = 9;\r\n    struct film bond[size] = {\r\n        { \"Casino Royale\", 2006 },\r\n        { \"Dr. No\", 1962 },\r\n        { \"GoldenEye\", 1995 },\r\n        { \"Goldfinger\", 1964 },\r\n        { \"Moonraker\", 1979 },\r\n        { \"Octopussy\", 1983 },\r\n        { \"Skyfall\", 2012 },\r\n        { \"Spectre\", 2015 },\r\n        { \"Thunderball\", 1965 }\r\n    };\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* sort the list by year *\/<\/span>\r\n    qsort(bond,size,sizeof(struct film),compare);\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n        printf(\"%d, %s\\n\",\r\n                bond[x].year,\r\n                bond[x].title\r\n              );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Even when I tried to compress the two statements in the <em>compare()<\/em> function, eliminating variables <code>one<\/code> and <code>two<\/code> and setting the conversions into the <em>return<\/em> statement, the program refused to compile. Only by declaring two <em>film<\/em> structures, converting the data in two statements, and then comparing the <code>year<\/code> members, did my solution work.<\/p>\n<p>It would thrill me to see another solution, something that uses a different approach. As I wrote in the original Exercise post, most programmers tend to copy-paste their <em>compare()<\/em> functions. Yet power exists in this function, part of the quick-sort process. Let me know if you also met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to extract a structure member from a void pointer for use in the quick-sort compare() function. This isn&#8217;t a solution you can readily find on the Interwebs.<\/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-5660","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\/5660","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=5660"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5660\/revisions"}],"predecessor-version":[{"id":5677,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5660\/revisions\/5677"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}