{"id":5917,"date":"2023-06-24T00:01:23","date_gmt":"2023-06-24T07:01:23","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5917"},"modified":"2023-06-17T10:49:42","modified_gmt":"2023-06-17T17:49:42","slug":"outputting-a-key-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5917","title":{"rendered":"Outputting a Key Value"},"content":{"rendered":"<p>The power of an associative array is that it uses keys, text or numbers, to reference values. Know the key and you can fetch the value . This &#8220;association&#8221; works like an array in C, where you know the index or offset for an element. Though with an associative array, the index is a value or string &mdash; the key.<br \/>\n<!--more--><br \/>\nFor my demonstration, I continue with an phony associative array of strings used in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5910\">last week&#8217;s Lesson<\/a>. The keys are month names in English with the values their corresponding month names in French. The <em>key()<\/em> function is passed two arguments: <code>*k<\/code> a key to match and <code>*a[]<\/code> the phony-associative array containing the key-value pairs. Here is the function:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* assumes `char` array *\/<\/span>\r\nchar *key(char *k, char *a[])\r\n{\r\n    int x = 0;\r\n\r\n    <span class=\"comments\">\/* the loop ends on the NULL *\/<\/span>\r\n    while( a[x] )\r\n    {\r\n        <span class=\"comments\">\/* use strcasecmp() if the library has it *\/<\/span>\r\n        if( strcmp(k,a[x])==0 )\r\n            return(a[x+1]);\r\n        x+=2;\r\n    }\r\n    return(NULL);\r\n}<\/pre>\n<p>The <em>while<\/em> loop spins until the end of array <code>a[]<\/code>, which is marked with <code>NULL<\/code>s. Within the loop, an <em>strcmp()<\/em> function compares the key passed, <code>*k<\/code>, with each key item in the array, <code>a[x]<\/code>. When a match is found, meaning <em>strcmp()<\/em> returns zero, element <code>a[x+1]<\/code> is returned. The <code>+1<\/code> offset represents the key&#8217;s value in the phony-associative array. Otherwise, variable <code>x<\/code> is increased by two to reference the next key in the phony associative array.<\/p>\n<p>When the array is exhausted, the value <code>NULL<\/code> is returned.<\/p>\n<p>I could have put the <em>strcmp()<\/em> function within the <em>while()<\/em> loop, but extra overhead is required to determine when the <code>a[x]<\/code> value is <code>NULL<\/code>. It seems less complicated to me to use value <code>a[x]<\/code> as the comparison.<\/p>\n<p>Also, if your compiler&#8217;s library features the <em>strcasecmp()<\/em> function, you can use it instead of <em>strcmp()<\/em>. Though, grammatically speaking, month names in English are capitalized.<\/p>\n<p>Within the <em>main()<\/em> function, input is requested. The input string is passed to the <em>key()<\/em> function. The value returned is compared with <code>NULL<\/code> and appropriate text is output:<\/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    char a[16];\r\n    char *b;\r\n\r\n    printf(\"Month in English: \");\r\n    <span class=\"comments\">\/* scanf() doesn't monitor overflow! *\/<\/span>\r\n    scanf(\"%s\",a);\r\n    b = key(a,months);\r\n    if( b!=NULL )\r\n        printf(\"'%s' is '%s' in French\\n\",a,b);\r\n    else\r\n        printf(\"'%s' isn't a month name\\n\",a);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I dislike using the <em>scanf()<\/em> function for any form of input, but it presents a single statement here instead of my having to craft my own input function. Array <code>a[]<\/code> is fixed to 16 characters (15 text plus the null character), but this limit isn&#8217;t tested by the <em>scanf()<\/em> function. If you release such code into the wild, ensure you use a proper text input function complete with bounds-checking.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_06_24-Lesson.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the full code on GitHub. Here&#8217;s a sample run:<\/p>\n<p><code>Month in English: May<br \/>\n'May' is 'mai' in French<\/code><\/p>\n<p>And a sample run with bogus input:<\/p>\n<p><code>Month in English: Bogus<br \/>\n'Bogus' isn't a month name<\/code><\/p>\n<p>By using an associative array, even a phony one in C, I&#8217;m able to finally reach the goal I desired in this series&#8217; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5901\">original post<\/a>: To create a pair of arrays to translate from English to French.<\/p>\n<p>This phony associative array concept can be expanded further, but not without some modification. Functions can be built to create the array, assigning keys and values. To accomplish this task, it&#8217;s better to create a linked list of dictionary structures with key and value members, as was suggested in an earlier comment in this series.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The beauty of an associative array is that you don&#8217;t need to know an element&#8217;s number, just its associated key name. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5917\">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-5917","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\/5917","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=5917"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5917\/revisions"}],"predecessor-version":[{"id":5929,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5917\/revisions\/5929"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}