{"id":6474,"date":"2024-07-20T00:01:26","date_gmt":"2024-07-20T07:01:26","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6474"},"modified":"2024-07-13T09:12:37","modified_gmt":"2024-07-13T16:12:37","slug":"merry-pointer-mischief","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6474","title":{"rendered":"Merry Pointer Mischief"},"content":{"rendered":"<p>Nothing paralyzes a C programmer like double-asterisk notation. What does it mean? Can you use it? How is it passed to a function and then referenced? I, too, fall victim to this confusion. So a good explanation is in order.<br \/>\n<!--more--><br \/>\nDouble-pointer notation is best demonstrated with an array of strings:<\/p>\n<p><code>char *words[] = {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"alpha\", \"beta\", \"gamma\", \"delta\", NULL<br \/>\n};<\/code><\/p>\n<p>This declaration represents an array of pointers, the memory locations of the various strings where they&#8217;re stored in memory. (I added NULL as the last item to identify the end of the list.)<\/p>\n<p>You can use array notation to reference each string: <code>words[1]<\/code> is <code>beta<\/code>, for example. You can also use pointer notation: <code>*(words+1)<\/code> is <code>beta<\/code>.<\/p>\n<p>When passing the array to a function, you can use <code>words<\/code> or <code>*words<\/code>. What&#8217;s passed and how the function deals with it is demonstrated in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_07_20-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_07_20-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid one(char **words)\r\n{\r\n    <span class=\"comments\">\/*\r\n         **words is the base of the array\r\n        **words is also the first string's first character\r\n        *words is the first string\r\n    *\/<\/span>\r\n    puts(\"You have passed the entire array:\");\r\n    while(*words)\r\n        puts(*words++);\r\n}\r\n\r\nvoid two(char *word)\r\n{\r\n    <span class=\"comments\">\/*\r\n       word is a string\r\n       *word is the first character of the string\r\n    *\/<\/span>\r\n    puts(\"You have passed a single string:\");\r\n    puts(word);\r\n}\r\n\r\nint main()\r\n{\r\n    char *words[] = {\r\n        \"alpha\", \"beta\", \"gamma\", \"delta\", NULL\r\n    };\r\n\r\n    one(words);\r\n    two(*words);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Function <em>one()<\/em> is passed <code>words<\/code>, which is the base of the pointer array. In the function&#8217;s declaration, the argument is written <code>**words<\/code>. The reason is that <code>words<\/code> in the <em>main()<\/em> function is the base of the array. In the <em>one()<\/em> function, <code>words<\/code> represents an array of pointers, which is why it&#8217;s referenced as <code>**words<\/code>.<\/p>\n<p>Within the <em>one()<\/em> function, variable <code>*words<\/code> represents a string in the array. The <em>while<\/em> loop outputs each word. If you use <code>**words<\/code> in the function, it represents the first character in the first string. Remember, <code>**words<\/code> in the function&#8217;s declaration describes the <em>type<\/em> of data passed. In the function, the double-asterisk represents a pointer-pointer: the address of the first string and the address of its first character.<\/p>\n<p>The <em>two()<\/em> function is passed <code>*words<\/code>, which in the <em>main()<\/em> function represents a single string. For the <em>two()<\/em> function declaration it represents a single string, <code>*word<\/code>. Within the function, <code>word<\/code> represents the string; <code>*word<\/code> is the first character of the string.<\/p>\n<p>All this asterisk nonsense can be so confusing! It&#8217;s easy to understand how any C programmer, not just a beginner, can be driven nuts with this stuff.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the difference between <code>*data<\/code> and <code>**data<\/code> as arguments in a function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6474\">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-6474","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\/6474","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=6474"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6474\/revisions"}],"predecessor-version":[{"id":6503,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6474\/revisions\/6503"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}