{"id":2505,"date":"2017-05-20T00:01:40","date_gmt":"2017-05-20T07:01:40","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2505"},"modified":"2017-05-13T09:52:56","modified_gmt":"2017-05-13T16:52:56","slug":"pass-a-2d-array-to-a-function-no-pointers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2505","title":{"rendered":"Pass a 2D Array to a Function (No Pointers!)"},"content":{"rendered":"<p>Suppose you have an array of integers which represents a magic square: All the rows and columns &mdash; even the two diagonals &mdash; add to the same total. To prove it, you create a function, <em>confirm_magic()<\/em> that processes the array and validates the math. You have just one problem . . .<br \/>\n<!--more--><br \/>\nIf you want to pass a two-dimensional array (a grid) to a function, you must use pointers. Specifically, you summon the dratted <code>**<\/code> thing, which everyone avoids.<\/p>\n<p>To keep your sanity, you instead decide to use a single-dimension array, which is how all arrays are stored internally anyhow. (See <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2498\">last week&#8217;s Lesson<\/a>.)<\/p>\n<p>Use this format to pass an array to a function:<\/p>\n<p><code>confirm_magic(array)<\/code><\/p>\n<p>where &#8220;array&#8221; otherwise appears as <code>array[]<\/code> in the code. And to declare an array as a function&#8217;s argument, you use this prototype:<\/p>\n<p><code>int confirm_magic(array[]);<\/code><\/p>\n<p>The array marches off to the function, is processed as a 2D array, and then the results are returned. Magic. Or not.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0520.c\">Click here<\/a> to view the entire code, which is lengthy, but contains a lot of similar pieces.<\/p>\n<p>The <code>grid[]<\/code> array is single-dimension:<\/p>\n<pre class=\"screen\">\r\n    int grid[25] = {\r\n        25, 13,  1, 19,  7,\r\n        16,  9, 22, 15,  3,\r\n        12,  5, 18,  6, 24,\r\n         8, 21, 14,  2, 20,\r\n         4, 17, 10, 23, 11\r\n    };<\/pre>\n<p>I formatted it to look like a 2D array, though it&#8217;s not declared as such.<\/p>\n<p>In the <em>main()<\/em> function, the code uses sleight-of-hand to display <code>grid[]<\/code> in rows and columns. Then the array is passed to the <em>confirm_magic()<\/em> function, where its rows, columns, and diagonal offsets are calculated and tallied. The value 1 or 0 is returned for TRUE or FALSE, indicating whether the grid is a magic square.<\/p>\n<p>Here is the rows calculation from the <em>confirm_magic()<\/em> function. The array is named <code>square[]<\/code> within the function:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* calculate rows *\/<\/span>\r\n    for(r=0;r&lt;5;r++)\r\n    {   \r\n        sum=0;\r\n        for(c=0;c&lt;5;c++)\r\n            sum+=square[r*5+c];\r\n        if(sum != 65)\r\n            return(0);\r\n    }<\/pre>\n<p>Variables <code>r<\/code> and <code>c<\/code> represent rows and columns (actually offsets) in the array. The specific code <code>square[r*5+c]<\/code> represents array elements from row <code>r<\/code>. So the first row, 0, is processed with columns (<code>c<\/code>) 0 through 4. Then comes row 1, and onward. Variable <code>sum<\/code> accumulates the values and is compared with 65, the desired total for this particular magic square. If the value isn&#8217;t 65, the function returns 0, FALSE.<\/p>\n<p>For the column calculation, the following statement is used:<\/p>\n<p><code>sum+=square[c+r*5];<\/code><\/p>\n<p>Variable <code>c<\/code> loops from 0 to 4, representing &#8220;columns&#8221; within the single-dimension array. Then the row value, <code>r<\/code>, loops from 0 to 4. The function&#8217;s math tallies the total of each &#8220;column&#8221; in the array, accumulating the total in variable <code>sum<\/code>.<\/p>\n<p>Similar loops process the diagonals, using math to determine which array elements fall on the diagnoal.<\/p>\n<p>Here is a sample run:<\/p>\n<pre><code>Magic Square\r\n  25  13   1  19   7\r\n  16   9  22  15   3\r\n  12   5  18   6  24\r\n   8  21  14   2  20\r\n   4  17  10  23  11\r\nYes, it's a magic square<\/code><\/pre>\n<p>If you alter the array, the <em>confirm_magic()<\/em> function returns FALSE and an appropriate message is output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To avoid using <code>**<\/code> notation, you can fake a 2D array as a single-dimension array to pass it to a function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2505\">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-2505","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\/2505","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=2505"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2505\/revisions"}],"predecessor-version":[{"id":2524,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2505\/revisions\/2524"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}