{"id":5910,"date":"2023-06-17T00:01:02","date_gmt":"2023-06-17T07:01:02","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5910"},"modified":"2023-06-24T07:41:59","modified_gmt":"2023-06-24T14:41:59","slug":"dumping-a-phony-associative-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5910","title":{"rendered":"Dumping a Phony Associative Array"},"content":{"rendered":"<p>The first phony associative array function I need to write is a simple dump: Output the array&#8217;s contents in the form of pairs. Performing this operation requires a bit of manipulation to the way the array is presented in the code.<br \/>\n<!--more--><br \/>\nStarting with the example from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5903\">last week&#8217;s Lesson<\/a>, I modified the array so that it&#8217;s a single dimension array of strings, yet presented as a series of pairs:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    char *months[] =\r\n    {\r\n        \"January\", \"janvier\",\r\n        \"February\", \"fevrier\",\r\n        \"March\", \"mars\",\r\n        \"April\", \"avril\",\r\n        \"May\", \"mai\",\r\n        \"June\", \"juin\",\r\n        \"July\", \"julliet\",\r\n        \"August\", \"aout\",\r\n        \"September\", \"septembre\",\r\n        \"October\", \"octobre\",\r\n        \"November\", \"novembre\",\r\n        \"December\", \"decembre\",\r\n        NULL, NULL\r\n    };\r\n\r\n    puts(\"Array Dump\");\r\n    array_dump(months);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The new phony-associative array looks cleaner, plus it&#8217;s easier to work with as a single dimension <em>char<\/em> pointer array. Specifically, look at the <em>array_dump()<\/em> function, which requires only the array&#8217;s name as an argument; no double-pointer nonsense. Though keep in mind that this function assumes the array&#8217;s contents are strings.<\/p>\n<p>Also note that the final pair in the array are <code>NULL<\/code>s. This termination allows me to be flexible with my <em>array_dump()<\/em> function, eliminating a size argument.<\/p>\n<p>To output or &#8220;dump&#8221; the array, the <em>array_dump()<\/em> function churns through each pair in the array, outputting the keys and values each on a line:<\/p>\n<pre class=\"screen\">\r\nvoid array_dump(char *a[])\r\n{\r\n    int x = 0;\r\n\r\n    while( a[x] )\r\n    {\r\n        printf(\"'%s' =&gt; '%s'\\n\",\r\n                a[x],\r\n                a[x+1]\r\n              );\r\n        x+=2;\r\n    }\r\n}<\/pre>\n<p>The function&#8217;s argument uses the same format as the <code>months[]<\/code> array declared in the <em>main()<\/em> function. Again, no double-asterisk nonsense.<\/p>\n<p>The <em>while<\/em> loop spins as long as the value of <code>a[x]<\/code> isn&#8217;t <code>NULL<\/code>. Reference variables <code>a[x]<\/code> and <code>a[x+1]<\/code> represent each pair in the phony-associative array. The <code>x+=2<\/code> statement notches up the offset to the next pointer pair in the array. Here&#8217;s sample output:<\/p>\n<p><code>Array Dump<br \/>\n'January' => 'janvier'<br \/>\n'February' => 'fevrier'<br \/>\n'March' => 'mars'<br \/>\n'April' => 'avril'<br \/>\n'May' => 'mai'<br \/>\n'June' => 'juin'<br \/>\n'July' => 'julliet'<br \/>\n'August' => 'aout'<br \/>\n'September' => 'septembre'<br \/>\n'October' => 'octobre'<br \/>\n'November' => 'novembre'<br \/>\n'December' => 'decembre'<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_06_17-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the full code on GitHub.<\/p>\n<p>Another dumping function commonly found in programming languages that deal with associative arrays just outputs the keys. Here is such a function, <em>key_dump()<\/em>:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* assumes `char` array *\/<\/span>\r\nvoid key_dump(char *k[])\r\n{\r\n    int x = 0;\r\n\r\n    while( k[x] )\r\n    {\r\n        printf(\"'%s'\\n\",\r\n                k[x]\r\n              );\r\n        x+=2;\r\n    }\r\n}<\/pre>\n<p>The <em>key_dump()<\/em> function is a slight modification of <em>array_dump()<\/em>, just eliminating the value part of the array&#8217;s output. Here&#8217;s a sample run:<\/p>\n<p><code>Key dump:<br \/>\n'January'<br \/>\n'February'<br \/>\n'March'<br \/>\n'April'<br \/>\n'May'<br \/>\n'June'<br \/>\n'July'<br \/>\n'August'<br \/>\n'September'<br \/>\n'October'<br \/>\n'November'<br \/>\n'December'<\/code><\/p>\n<p>You can view the full code on <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_06_17-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">GitHub here<\/a>.<\/p>\n<p>For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5917\">next week&#8217;s Lesson<\/a>, I craft a <em>key()<\/em> function, which returns the value part of the phony-associative array when the key is supplied.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the first associative array functions (methods) is the dump. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5910\">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-5910","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\/5910","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=5910"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5910\/revisions"}],"predecessor-version":[{"id":5936,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5910\/revisions\/5936"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}