{"id":6802,"date":"2025-02-08T00:02:41","date_gmt":"2025-02-08T08:02:41","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6802"},"modified":"2025-02-01T12:39:17","modified_gmt":"2025-02-01T20:39:17","slug":"testing-your-pointer-prowess-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6802","title":{"rendered":"Testing Your Pointer Prowess &#8211; Solution"},"content":{"rendered":"<p>The solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6791\">this month&#8217;s Exercise<\/a> is to output the string <code>\"I am an excellent C programmer\"<\/code>. This task isn&#8217;t so simple when you must untie the knot of the dratted double pointer.<br \/>\n<!--more--><br \/>\nThe challenge presents four arrays, with array <code>z[]<\/code> the only one you can use to generate the string:<\/p>\n<p><code>char *a[] = { \"I\", \"good\", \"language\" };<br \/>\nchar *b[] = { \"am\", \"C\", \"excellent\", \"an\" };<br \/>\nchar *c[] = { \"a\", \"programmer\", \"very\" };<br \/>\nchar **z[] = { a, b, c };<\/code><\/p>\n<p>The key is to reference each array within <code>z[]<\/code> &mdash; <code>a[]<\/code>, <code>b[]<\/code>, and <code>c[]<\/code> &mdash; by using double pointer notation to access the strings. Scary! But as I wrote in the challenge post, once you get the first construction down the rest come easy.<\/p>\n<p>Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_02-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_02-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *a[] = { \"I\", \"good\", \"language\" };\r\n    char *b[] = { \"am\", \"C\", \"excellent\", \"an\" };\r\n    char *c[] = { \"a\", \"programmer\", \"very\" };\r\n    char **z[] = { a, b, c };\r\n\r\n    printf(\"%s %s %s %s %s %s\\n\",\r\n            *(*(z+0)+0),\r\n            *(*(z+1)+0),\r\n            *(*(z+1)+3),\r\n            *(*(z+1)+2),\r\n            *(*(z+1)+1),\r\n            *(*(z+2)+1)\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>To unwind the string references in the <em>printf()<\/em> statement, start from the inside and work your way out. Each array referenced by <code>z[]<\/code> has an offset:<\/p>\n<p><code>a[]<\/code>&#8217;s address is <code>z+0<\/code><br \/>\n<code>b[]<\/code>&#8217;s address is <code>z+1<\/code><br \/>\n<code>c[]<\/code>&#8217;s address is <code>z+2<\/code><\/p>\n<p>These values are addresses because <code>z<\/code> is a double-pointer: <code>**z<\/code>. To access the array&#8217;s data, you must dereference the address:<\/p>\n<p><code>*(z+0)<\/code> is <code>a[]<\/code><br \/>\n<code>*(z+1)<\/code> is <code>b[]<\/code><br \/>\n<code>*(z+2)<\/code> is <code>c[]<\/code><\/p>\n<p>After dereferencing the array, the next offset represents a string&#8217;s address within the array. For example:<\/p>\n<p><code>*(z+0)+0<\/code> is the address of string <code>\"I\"<\/code>, the first string in <code>a[]<\/code><br \/>\n<code>*(z+1)+1<\/code> is the address of string <code>\"C\"<\/code>, the second string in <code>b[]<\/code><br \/>\n<code>*(z+2)+2<\/code> is the address of string <code>\"very\"<\/code>, the third string in <code>c[]<\/code><\/p>\n<p>As these expressions resolve to addresses, they must be further dereferenced with <code>*<\/code> to access a specific string&#8217;s contents, as well as to match the <code>%s<\/code> placeholder in the <em>printf()<\/em> statement:<\/p>\n<p><code>*(*(z+0)+0)<\/code> is the string <code>\"I\"<\/code><br \/>\n<code>*(*(z+1)+0)<\/code> is the string <code>\"am\"<\/code><br \/>\n<code>*(*(z+1)+3)<\/code> is the string <code>\"an\"<\/code><br \/>\n<code>*(*(z+1)+2)<\/code> is the string <code>\"excellent\"<\/code><br \/>\n<code>*(*(z+1)+1)<\/code> is the string <code>\"C\"<\/code><br \/>\n<code>*(*(z+2)+1)<\/code> is the string <code>\"programmer\"<\/code><\/p>\n<p>That&#8217;s the solution.<\/p>\n<p>I hope your solution also met with success. Double-pointers happen often in C and you can&#8217;t easily avoid them by trying to fake it with array notation. The more you practice, the better you get at understanding and using this useful convention.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The solution for this month&#8217;s Exercise is to output the string &#8220;I am an excellent C programmer&#8221;. This task isn&#8217;t so simple when you must untie the knot of the dratted double pointer.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6802","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6802","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=6802"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6802\/revisions"}],"predecessor-version":[{"id":6817,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6802\/revisions\/6817"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}