{"id":7539,"date":"2026-05-01T00:01:36","date_gmt":"2026-05-01T07:01:36","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7539"},"modified":"2026-04-25T12:03:40","modified_gmt":"2026-04-25T19:03:40","slug":"pointers-to-multi-dimension-arrays","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7539","title":{"rendered":"Pointers to Multi-Dimension Arrays"},"content":{"rendered":"<p>I received a question via email regarding my recently-published book, <em>Dan Gookin&#8217;s Guide to C Language Pointers<\/em> (available at <a href=\"https:\/\/amzn.to\/4vgTkAA\" target=\"_new\">Amazon<\/a>!). The question: How to use a pointer to navigate a multi-dimension array? While I do cover pointers to an array of strings (which is kinda what the reader wanted), I don&#8217;t specifically cover pointers and multi-dimensional arrays. Therefore, I created this month&#8217;s exercise to tackle the task.<br \/>\n<!--more--><br \/>\nMulti-dimensional arrays are useful for representing data in grids or matrixes. But the array notation used in C is only for your benefit; internally, the data is stored sequentially. The compiler does the math to ensure that the proper element is referenced no matter what dimensions are specified. Translating this data into pointers is a matter of approach coupled with various math thingies.<\/p>\n<p>To start, I present the following code, which populates a two-dimensional array with random data:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_05_01-Lesson.c<\/a><\/h3>\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 ROW 4\r\n#define COL 3\r\n\r\nint main()\r\n{\r\n    int values[ROW][COL];\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* populate and output the grid *\/<\/span>\r\n    for( y=0; y&lt;ROW; y++ )\r\n    {\r\n        for( x=0; x&lt;COL; x++ )\r\n        {\r\n            values[y][x] = rand() % 100;\r\n            printf(\"[%d][%d] = %d\\t\",y,x,values[y][x]);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The program&#8217;s output changes with each run, but here&#8217;s a sample:<\/p>\n<pre>[0][0] = 36     [0][1] = 70     [0][2] = 65\r\n[1][0] = 14     [1][1] = 27     [1][2] = 99\r\n[2][0] = 13     [2][1] = 62     [2][2] = 4\r\n[3][0] = 42     [3][1] = 54     [3][2] = 9<\/pre>\n<p>Your task for this month&#8217;s Exercise is to use one or more pointers to navigate through the matrix and output the data.<\/p>\n<p>In my solution, I output the data three times in addition to the initial output above:<\/p>\n<ol>\n<li>I use a single point to output the values.<\/li>\n<li>I use a single pointer, but output the values in a grid format.<\/li>\n<li>I use two pointers to work through each dimension of the array.<\/li>\n<\/ol>\n<p>The way you manage your solution is up to you, though I&#8217;ll present all three in mine.<\/p>\n<p>As a hefty tip, be aware that obtaining the base address of a multi-dimension array isn&#8217;t as straightforward as obtaining the address of a single-dimension array. For a typical array, such as <code>array[]<\/code>, you can assign pointer <code>a<\/code> to the address by using the following statement:<\/p>\n<p><code>a = array;<\/code><\/p>\n<p>This approach doesn&#8217;t work for multi-dimensional arrays. I don&#8217;t know if there is an established trick to make the assignment, so what I use is the following:<\/p>\n<p><code>a = array[0][0];<\/code><\/p>\n<p>Pointer <code>a<\/code> is assigned the address of the array&#8217;s first element. As arrays are stored sequentially in memory, this approach works. Feel free to use it in your code, unless you can think of something better.<\/p>\n<p>I&#8217;ll post my solution in a week.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using a pointer to access multi-dimension array data betrays the ugly truth about arrays. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7539\">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":[3],"tags":[],"class_list":["post-7539","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7539","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=7539"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7539\/revisions"}],"predecessor-version":[{"id":7580,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7539\/revisions\/7580"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}