{"id":562,"date":"2014-04-05T00:01:56","date_gmt":"2014-04-05T08:01:56","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=562"},"modified":"2014-03-29T08:59:49","modified_gmt":"2014-03-29T15:59:49","slug":"manually-allocating-a-pointer-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=562","title":{"rendered":"Manually Allocating a Pointer Array"},"content":{"rendered":"<p>A few Lessons ago, I mentioned that a variable such as <code>**months<\/code> couldn&#8217;t be used to declare an array. That&#8217;s true because the <code>**months<\/code> construction doesn&#8217;t use array notation. Duh. That doesn&#8217;t mean that <code>**months<\/code> is totally out of luck.<br \/>\n<!--more--><br \/>\nBefore venturing any further, I understand that this topic is insanely advanced. It&#8217;s utterly acceptable if you utterly want to eschew any variable in the C language that begins with two asterisks. Feel free to merrily skip this Lesson. La-di-da.<\/p>\n<p>Buried in the bosom of digital storage, an array of strings is really a collection of memory locations or addresses. To code those addresses in the C language, you can create a pointer array. For example:<\/p>\n<pre><code>char *dwarfs[7] = {\r\n    \"Bashful\",\r\n    \"Doc\",\r\n    \"Dopey\",\r\n    \"Grumpy\",\r\n    \"Happy\",\r\n    \"Sleepy\",\r\n    \"Sneezy\" };<\/code><\/pre>\n<p>The <code>*dwarfs[]<\/code> array isn&#8217;t a two-dimensional string array; it&#8217;s an array of pointers. Each pointer shows the starting location in memory of the seven strings.<\/p>\n<p>If you were to examine the <code>*dwarfs[]<\/code> array in memory, it would look something like Figure 1, which is how the Code::Blocks debugger displays such a beast.<\/p>\n<div id=\"attachment_628\" style=\"width: 492px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-628\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure1.png\" alt=\"Figure 1. A dump of the *dwarfs[] array. It lists the memory locations (pointers) to each string in the array.\" width=\"482\" height=\"250\" class=\"size-full wp-image-628\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure1.png 482w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure1-300x155.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><p id=\"caption-attachment-628\" class=\"wp-caption-text\">Figure 1. A dump of the <code>*dwarfs[]<\/code> array. It lists the memory locations (pointers) to each string in the array.<\/p><\/div>\n<p>For comparison purposes, there is the same data constructed as a two-dimensional <em>char<\/em> array:<\/p>\n<pre><code>char dwarfs[7][8] = {\r\n    \"Bashful\",\r\n    \"Doc\",\r\n    \"Dopey\",\r\n    \"Grumpy\",\r\n    \"Happy\",\r\n    \"Sleepy\",\r\n    \"Sneezy\" };<\/code><\/pre>\n<p>The string Bashful is the longest, requiring 8 bytes of storage (7 characters and one place for the <code>\\0<\/code>). Therefore, the second dimension of the array must be 8 characters for all seven elements. A memory dump of how the array is stored is shown in Figure 2.<\/p>\n<div id=\"attachment_648\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-648\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure2.png\" alt=\"Figure 2. Storage for of an array of strings.\" width=\"588\" height=\"192\" class=\"size-full wp-image-648\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure2.png 588w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure2-300x97.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0405-figure2-500x163.png 500w\" sizes=\"auto, (max-width: 588px) 100vw, 588px\" \/><p id=\"caption-attachment-648\" class=\"wp-caption-text\">Figure 2. Storage for of an array of strings.<\/p><\/div>\n<p>As you can see, it&#8217;s more wasteful to store the strings as an array of text characters. In Figure 2, you see a lot of null characters (<code>\\0<\/code>, which appears as code <code>00<\/code> in the memory dump). It&#8217;s far more efficient to store memory locations, which reference the strings stored one after the other elsewhere in memory.<\/p>\n<p>Of course, these days, conserving a few bytes of storage isn&#8217;t a major concern.<\/p>\n<p>Actually, to get back to the point, when storing an array of strings as memory locations, you can &#8212; if you&#8217;re crazy enough &#8212; build those strings first and then pack their memory locations into an array. The entire operation can be done without using array notation. That&#8217;s where the <code>**<\/code> notation comes into play:<\/p>\n<p>The variable type <code>**dwarfs<\/code> is similar to <code>*dwarfs[]<\/code> in many respects. One of the big differences is that storage for <code>**dwarfs<\/code> must be allocated, either by assigning that variable to an already-declared pointer array or by using the <em>malloc()<\/em> function to set aside storage. The following code demonstrates how that second solution works:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char *dwarf0 = \"Bashful\";\r\n    char *dwarf1 = \"Doc\";\r\n    char *dwarf2 = \"Dopey\";\r\n    char *dwarf3 = \"Grumpy\";\r\n    char *dwarf4 = \"Happy\";\r\n    char *dwarf5 = \"Sleepy\";\r\n    char *dwarf6 = \"Sneezy\";\r\n    char **dwarfs;\r\n    int x;\r\n\r\n    dwarfs = (char **)malloc(sizeof(dwarf0) * 7);\r\n    if(dwarfs == NULL)\r\n    {\r\n        puts(\"Unable to allocate pointer array\");\r\n        exit(1);\r\n    }\r\n\r\n    *(dwarfs+0) = dwarf0;\r\n    *(dwarfs+1) = dwarf1;\r\n    *(dwarfs+2) = dwarf2;\r\n    *(dwarfs+3) = dwarf3;\r\n    *(dwarfs+4) = dwarf4;\r\n    *(dwarfs+5) = dwarf5;\r\n    *(dwarfs+6) = dwarf6;\r\n\r\n    for(x=0;x&lt;7;x++)\r\n        printf(\"Dwarf %d is %s\\n\",x+1,*(dwarfs+x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this code, the strings are declared by themselves in Lines 6 through 14. Each of the string variables represent memory locations &#8212; pointers.<\/p>\n<p>At Line 16, memory is allocated to store 7 pointer variables. The <em>sizeof<\/em> operator obtains the storage required for a <em>char<\/em> pointer. That value is multiplied by 7. The result of the <em>malloc()<\/em> function is typecast to reference an array of pointers, which is what the <code>(char **)<\/code> thingy does. The result is assigned to the <code>dwarfs<\/code> variable, which is also a <code>**<\/code> type of variable.<\/p>\n<p>Yeah, it&#8217;s messy, but it does work and if you know your pointers, it makes sense.<\/p>\n<p>Once the storage is allocated, the pointers from each individual string can be assigned to <code>**dwarfs<\/code>. Array notation isn&#8217;t used because (once again) <code>**dwarfs<\/code> isn&#8217;t an array; it&#8217;s a pointer-to-a-pointer thing. Therefore the pointer equivalent notation is used in Lines 23 through 29. (By &#8220;pointer equivalent&#8221; I mean pointer notation that&#8217;s equivalent to array notation.)<\/p>\n<p>Finally, to prove that it all worked, a <em>for<\/em> loop displays the &#8220;array&#8221; at Line 31.<\/p>\n<p>Beyond this example, and code similar to it, you really don&#8217;t see the <code>**<\/code> type of variable used that often. Next Lesson I&#8217;ll present perhaps the most common and practical example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While you cannot use the <code>**blorf<\/code> format to declare an array of strings, you can use it to build an array of strings. If that sounds confusing, relax: It <em>is<\/em> confusing. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=562\">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-562","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\/562","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=562"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions\/650"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}