{"id":394,"date":"2016-01-21T15:53:17","date_gmt":"2016-01-21T19:53:17","guid":{"rendered":"http:\/\/www.tayloraliss.com\/blog\/?p=394"},"modified":"2016-01-21T16:06:25","modified_gmt":"2016-01-21T20:06:25","slug":"a-pointer-to-a-pointer-to-a-pointer-to-a","status":"publish","type":"post","link":"https:\/\/www.tayloraliss.com\/blog\/?p=394","title":{"rendered":"A pointer to a pointer to a pointer to a&#8230;"},"content":{"rendered":"<p>This is a small slice of code adapted form a school project I was working on that I feel really helps explain the concept of pointers with arrays. It&#8217;s a 2-D\u00a0array: an array of pointers, each of which\u00a0points to an array of ints. It essentially forms a 2&#215;2 grid (but you can make it other sizes too).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"enlighter\">\/\/function prototype\r\nvoid fillMatrix(int** myArray, int size);\r\n\r\n\/\/main() function\r\nint main()\r\n{\r\n    const int SIZE = 4;\r\n    int** p_myArray = new int*[2]; \/\/make the initial x-axis which has two slots\r\n    p_myArray[0] = new int[2]; \/\/make the 1st y-axis which has two slots\r\n    p_myArray[1] = new int[2]; \/\/make the 2nd y-axis which has two slots\r\n\r\n    \/\/alternatively, you could do it this way (if making a 3x3 grid with SIZE=3):\r\n    \/*\r\n    int** p_myArray = new int*[3];                                     \r\n    for (int i = 0; i &lt; 4; i++) \/\/for every x-axis...\r\n    {\r\n        p_myArray[i] = new int[3]; \/\/make a y-axis three slots big                  \r\n    }\r\n    *\/\r\n\r\n    \/\/function call\r\n    fillMatrix(p_myArray, SIZE);\r\n}\r\n\r\n\/\/function definition\r\nvoid fillMatrix(int** myArray, int size)\r\n{\r\n cout &lt;&lt; \"Please enter \" &lt;&lt; size &lt;&lt; \" integers to fill the array.\" &lt;&lt; endl;\r\n for (int i = 0; i &lt; (2); i++)\r\n     {\r\n         cin &gt;&gt; myArray[0][i];\r\n     }\r\n}<\/pre>\n<p>Just to help visualize it, here is what a 2-D, 5&#215;5 array looks like:<\/p>\n<p><a href=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2016\/01\/M75kn.png\" rel=\"attachment wp-att-405\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-405 size-full\" src=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2016\/01\/M75kn.png\" alt=\"M75kn\" width=\"454\" height=\"324\" srcset=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2016\/01\/M75kn.png 454w, https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2016\/01\/M75kn-300x214.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a small slice of code adapted form a school project I was working on that I feel really helps explain the concept of pointers with arrays. It&#8217;s a 2-D\u00a0array: an array of pointers, each of which\u00a0points to an array of ints. It essentially forms a 2&#215;2 grid (but you can make it other [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394"}],"version-history":[{"count":10,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":409,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions\/409"}],"wp:attachment":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}