{"id":2484,"date":"2017-05-06T00:01:08","date_gmt":"2017-05-06T07:01:08","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2484"},"modified":"2017-05-13T07:50:10","modified_gmt":"2017-05-13T14:50:10","slug":"two-dimensional-arrays-are-a-myth","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2484","title":{"rendered":"Two Dimensional Arrays are a Myth"},"content":{"rendered":"<p>Do C language pointers frighten you? Good! They&#8217;re supposed to, mostly because few instructors bother explaining them well, but also because of the nomenclature: &#8220;Pointers point.&#8221; Regardless, if you shun pointers, as many C programmers do, you can fall back on array notation. It&#8217;s a useful alternative and a handy shortcut, but it&#8217;s completely bogus.<br \/>\n<!--more--><br \/>\nIn other programming languages, those that borrow C syntax and notation, arrays are most likely 100 percent legit. In C, however, they&#8217;re a myth. This myth comes into play specifically for multi-dimensional arrays. To wit:<\/p>\n<p><code>int table[3][2];<\/code><\/p>\n<p>Whether you believe <em>int<\/em> array variable <code>table<\/code> to have 3 rows or 2 columns or 3 columns of 2 rows is irrelevant. Internally, the compiler sees only an <em>int<\/em> array with six elements. The expressions that access those elements are handy for you, but translate to the proper offset when the program runs. In other words, the compiler is just being nice.<\/p>\n<p>The following code contains a 5-by-5 <em>int<\/em> array, <code>grid<\/code>. It&#8217;s a &#8220;magic square,&#8221; which contains values 1 through 25 in such a way that rows, columns &mdash; even diagonals &mdash; sum to the same value, 65.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int grid[5][5] = {\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    };\r\n    int row,col;\r\n\r\n    puts(\"Magic Square\");\r\n    for(row=0;row&lt;5;row++)\r\n    {\r\n        for(col=0;col<5;col++)\r\n            printf(\"  %2d\",grid[row][col]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code is more readable as a two-dimensional array, using variables <code>row<\/code> and <code>col<\/code> to plug in values for the array <code>grid<\/code>. Internally, however, all that notation is translated into a single-dimension array; the <code>[row]<\/code> and <code>[col]<\/code> pieces merely calculate offsets within the single dimension array. Yes, you have to code more overhead if you plot the array that way, so why bother?<\/p>\n<p>Well, because I have to prove it, which is why I bother.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/0506b.c\">Click here<\/a> to view a modified version of the code, which plods through and adds all the rows, columns, and both diagonals to confirm that the array is indeed a magic square.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2498\">next week's Lesson<\/a>, I present the same code as a single-dimensional array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2D or not 2D. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2484\">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-2484","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\/2484","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=2484"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2484\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2484\/revisions\/2513"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}