{"id":123,"date":"2013-06-22T00:01:28","date_gmt":"2013-06-22T08:01:28","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=123"},"modified":"2013-06-22T07:55:09","modified_gmt":"2013-06-22T15:55:09","slug":"hi-low","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=123","title":{"rendered":"Hi-Low"},"content":{"rendered":"<p>Here&#8217;s a brain puzzle for you: Write a function that accepts a numeric array and returns the high and low values from that array. Can it be done?<br \/>\n<!--more--><br \/>\nOf course it can be done! I&#8217;ve yet to encounter a programming puzzle that can&#8217;t be cracked. Doing it well or doing it sloppy is the only issue.<\/p>\n<p>What&#8217;s sloppy? Global variables.<\/p>\n<p>Then how can you return two values from a function?<\/p>\n<p>Sure, you could write a function that fills a structure with both high and low elements of the array. To make a structure work with a function, however, you have to declare it as a global variable. Sloppy.<\/p>\n<p>The real solution is to use pointers. The function accepts three arguments: The array, of course, and then two addresses, one for the high value and the other for the low value. Here&#8217;s your sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define SIZE 25                 \/* size of the array *\/\r\n\r\nvoid hilo(int *a, int *h, int *l);\r\nvoid fill_array(int *a);\r\n\r\nint main()\r\n{\r\n    int array[SIZE];\r\n    int x,high,low;\r\n\r\n    fill_array(array);          \/* assign random values to the array *\/\r\n\r\n    for(x=0;x&lt;SIZE;x++)         \/* display array *\/\r\n        printf(\"%d\\t\",array[x]);\r\n    putchar('\\n');\r\n                                \/* Get high and low values *\/\r\n    hilo(array,&high,&low);\r\n    printf(\"The highest value is %d.\\n\",high);\r\n    printf(\"The lowest value is %d.\\n\",low);\r\n\r\n    return(0);\r\n}\r\n\r\n\/* Fetch the highest and lowest values in the array *\/\r\n\r\nvoid hilo(int *a, int *h, int *l)\r\n{\r\n    int x = 0;                  \/* initalize x to zero *\/\r\n\r\n    *h = *l = a[x];             \/* Initialize high\/low values *\/\r\n    for(;x&lt;SIZE;x++)            \/* x is already initialized *\/\r\n    {\r\n        if(a[x] &lt; *l)           \/* new low value? *\/\r\n            *l = a[x];          \/*  set new low value *\/\r\n        if(a[x] &gt; *h)           \/* new high value? *\/\r\n            *h = a[x];          \/*  set new high value *\/\r\n    }\r\n}\r\n\r\n\/* Put random values 1 through 100 into the array *\/\r\n\r\nvoid fill_array(int *a)\r\n{\r\n    int x;\r\n\r\n    srandom((unsigned)time(NULL));  \/* Seed the randomizer *\/\r\n\r\n    for(x=0;x&lt;SIZE;x++)\r\n    {\r\n        a[x] = random() % 100;      \/* Get random int value 0 to 99 *\/\r\n        a[x]++;                     \/* add one to equal 1 to 100 *\/\r\n    }\r\n}<\/pre>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>74\t71\t28\t5\t21\t52\t86\t67\t12\t94\r\n58\t58\t74\t10\t98\t95\t29\t82\t31\t76\r\n99\t21\t37\t57\r\nThe highest value is 99.\r\nThe lowest value is 5.\r\n<\/code><\/pre>\n<p>The meat of the code is the <em>hilo()<\/em> function, which uses dratted pointers to &#8220;return&#8221; the high and low values. Let me explain how that works.<\/p>\n<p><code>int<\/code> variables <code>high<\/code> and <code>low<\/code> are declared at Line 13. They&#8217;re regular <code>int<\/code> variables, not pointers. When those variables are passed to the <em>hilo()<\/em> function at Line 21, however, only their addresses (memory locations) are sent, thanks to the <code>&<\/code> prefix operator.<\/p>\n<p>Remember: <em>A pointer is a variable that stores a memory location<\/em>. So the effect of passing the <code>&high<\/code> and <code>&low<\/code> memory locations is that the function receives those variables as pointers. The function definition shows the variables passed defined that way:<\/p>\n<p><code>void hilo(int *a, int *h, int *l)<\/code><\/p>\n<p>Within the <em>hilo()<\/em> function, both variables <code>h<\/code> and <code>l<\/code> represent the memory locations of the <code>high<\/code> and <code>low<\/code> variables. Those variables must be prefixed by the <code>*<\/code> operator to access the variables&#8217; contents. The end effect is that the values assigned to <code>*h<\/code> and <code>*l<\/code> within <em>hilo()<\/em> directly affect the original variables in <em>main()<\/em>. There&#8217;s no need to &#8220;return&#8221; anything.<\/p>\n<p>The <code>a[x]<\/code> variable doesn&#8217;t need to be prefixed by <code>*<\/code> within the <em>hilo()<\/em> function because it&#8217;s an array. The compiler is just being nice to you by letting you use array notation. Internally, it sees <code>*(a+x)<\/code>.<\/p>\n<p>The method of returning a value by using a pointer explains why some functions specify pointers as arguments, such as <em>scanf()<\/em>. That way the function directly affects a variable&#8217;s value and doesn&#8217;t need to return something. Or, in the case of <em>hilo()<\/em>, it returns multiple values.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functions in C return only one value, but it&#8217;s possible to have them return many values. The trick is to use pointers. Yeah, those scary, nasty pointers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=123\">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-123","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\/123","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=123"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}