{"id":441,"date":"2014-02-08T00:01:31","date_gmt":"2014-02-08T08:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=441"},"modified":"2014-05-06T08:08:23","modified_gmt":"2014-05-06T15:08:23","slug":"an-array-of-pointers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=441","title":{"rendered":"An Array of Pointers"},"content":{"rendered":"<p>Here&#8217;s a scary thing for most people: <code>**blorf<\/code>. No, not the name <em>blorf<\/em>, although it&#8217;s one of my favorite made-up words. What drives programmers insane &#8212; and all programmers, not just C programmers &#8212; are those double disaster asterisks.<br \/>\n<!--more--><br \/>\nA pointer is a variable that holds a memory location, specifically the address of another variable.<\/p>\n<p>The pointer-to-a-pointer thing is confusing, I confess, but it doesn&#8217;t need to be. As an example, consider the following code, which uses a pointer array <code>months<\/code> to create a buncha strings:<\/p>\n<pre class=\"screen\">\r\n#include <stdio.h>\r\n\r\nint main()\r\n{\r\n    char *months[] = {\r\n        \"January\", \"February\", \"March\", \"April\",\r\n        \"May\", \"June\", \"July\", \"August\",\r\n        \"September\", \"October\", \"November\", \"December\" };\r\n    int x;\r\n\r\n    for(x=0;x&lt;12;x++)\r\n        printf(\"%s\\n\",months[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>An array of pointers is created at Line 5. It&#8217;s a common shortcut used by many programmers who need an array of strings. It&#8217;s not really an array of strings, however: It&#8217;s a list of 12 memory locations, each of which is the starting address of a string stored in memory.<\/p>\n<p>Figure 1 is a screenshot from the Code::Blocks debugger. It illustrates how the memory locations, or pointer values, are stored in the array.<\/p>\n<div id=\"attachment_463\" style=\"width: 506px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-463\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/1221-figure1.png\" alt=\"Figure 1. How the months array looks in memory.\" width=\"496\" height=\"290\" class=\"size-full wp-image-463\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/1221-figure1.png 496w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/1221-figure1-300x175.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><p id=\"caption-attachment-463\" class=\"wp-caption-text\">Figure 1. How the <code>months<\/code> array looks in memory.<\/p><\/div>\n<p>As you can see in Figure 1, each element of the array is an address, a memory location. The first element, <code>months[0]<\/code>, holds the address <code>0x403024<\/code>, which references the string &#8220;January.&#8221;<\/p>\n<p>The code displays the strings by using the <em>printf()<\/em> function at Line 12, courtesy of the <em>for<\/em> loop at Line 11. Simple, standard fare.<\/p>\n<p>Well, it looks simple because array notation is used. Yet, <code>months<\/code> is really a list of addresses, pointers. Just like the <code>*argv[]<\/code> argument for the <em>main()<\/em> function, it can also be written as <code>**months<\/code>.<\/p>\n<blockquote><p>Please do not flee in terror!<\/p><\/blockquote>\n<p>The <code>**months<\/code> construction works because <code>months<\/code> is a pointer and it references other pointers. An array is pretty-much a gussied up pointer. Therefore, the <code>months<\/code> variable itself references the starting address of a string. That&#8217;s how it can be used to display the strings; <code>months<\/code> references each string&#8217;s starting address.<\/p>\n<p>Let that sink in as you view this next code sample. Line 12 (the <em>printf()<\/em> function) substitutes a pointer for the array notation used in the original example:<\/p>\n<pre class=\"screen\">\r\n#include <stdio.h>\r\n\r\nint main()\r\n{\r\n    char *months[] = {\r\n        \"January\", \"February\", \"March\", \"April\",\r\n        \"May\", \"June\", \"July\", \"August\",\r\n        \"September\", \"October\", \"November\", \"December\" };\r\n    int x;\r\n\r\n    for(x=0;x&lt;12;x++)\r\n        printf(\"%s\\n\",*(months+x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The variable <code>*(months+x)<\/code> walks through the list of addresses stored in the <code>months<\/code> array. It starts with <code>0x403024<\/code> (from Figure 1) and then goes to <code>0x40302c<\/code>, and so on.<\/p>\n<p>Now you can&#8217;t use <code>**months<\/code> to declare an array of strings. That&#8217;s because an array must be declared by using array notation. So although <code>**months<\/code> and <code>*months[]<\/code> are equivalent elsewhere in the code, <code>**months<\/code> isn&#8217;t an array and cannot be used to declare one. To review:<\/p>\n<p><code>**months<\/code> is a pointer variable that holds the value of another pointer variable.<\/p>\n<p><code>*months[]<\/code> is a pointer variable that references an array of pointer variables.<\/p>\n<p>To help illustrate how this nonsense applies you can bring in another pointer-to-a-pointer variable, a second dratted <code>**<\/code> thing, such as the variable <code>**blorf<\/code>.<\/p>\n<p>The following code demonstrates how <code>**months<\/code> and <code>*months[]<\/code> are equivalent:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *months[] = {\r\n        \"January\", \"February\", \"March\", \"April\",\r\n        \"May\", \"June\", \"July\", \"August\",\r\n        \"September\", \"October\", \"November\", \"December\" };\r\n    int x;\r\n    char **blorf;      \/* here is the bastard *\/\r\n\r\n    blorf = months;\r\n    for(x=0;x&lt;12;x++)\r\n        printf(\"%s\\n\",*(blorf+x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Pointer variable <code>**blorf<\/code> is declared at Line 10. It&#8217;s a pointer-to-a-pointer variable, as shown by the dang double <code>*<\/code> operators.<\/p>\n<p>In Line 12, variable <code>blorf<\/code> is initialized to variable <code>months<\/code>. Both variables are of the same type &#8212; <code>**blorf<\/code> and <code>**months<\/code>, two <em>char<\/em> pointer-to-pointer variables &#8212; so the <code>&<\/code> (address-of) unary operator isn&#8217;t needed. (Both variable already hold memory locations.) After that, <code>blorf<\/code> can substitute for <code>months<\/code>, which it does at Line 14 to display the array of strings.<\/p>\n<p>Confused? Probably. Don&#8217;t sweat it. This kind of notation is rare. Despite that rarity, I stubbornly continue this pointless pointer-to-pointer discussion in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=467\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to pointers from beyond the first dimension! If the <em>*ptr<\/em> thing scares you, then <em>**ptr<\/em> can turn you to stone. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=441\">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-441","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\/441","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=441"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":714,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions\/714"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}