{"id":2329,"date":"2017-02-04T00:01:38","date_gmt":"2017-02-04T08:01:38","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2329"},"modified":"2017-02-11T08:14:23","modified_gmt":"2017-02-11T16:14:23","slug":"manipulate-pointers-in-functions-part-iv","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2329","title":{"rendered":"Manipulate Pointers in Functions, Part IV"},"content":{"rendered":"<p>Those programmers I wrote about in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2310\">last week&#8217;s Lesson<\/a>, the ones who avoid the <code>**<\/code> pointer notation, usually do so by using a two-dimensional array instead of the <code>**<\/code> pointer thing. It&#8217;s a quick substitute, but it&#8217;s not exactly the same thing.<br \/>\n<!--more--><br \/>\nAs an example, here&#8217;s the code from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2306\">Part II<\/a> in this series:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fourchar(char **p)\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;4;x++)\r\n    {\r\n        putchar(**p);\r\n        (*p)++;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char *text = \"ABCD\\n\";\r\n\r\n    fourchar(&amp;text);\r\n    putchar(*text);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>If you just want to display four characters in a string, you could re-write the code like this:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fourchar(char p[])\r\n{\r\n    int x;\r\n\r\n    for(x=0;x&lt;4;x++)\r\n    {\r\n        putchar(p[x]);\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char text[] = \"ABCD\\n\";\r\n\r\n    fourchar(text);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Suppose, however, that <code>text<\/code> were a two-dimensional array. For example, it&#8217;s declared as:<\/p>\n<p><code>char text[2][5] = { \"ABCD\", \"WXYZ\" };<\/code><\/p>\n<p>You need to pass <code>text[0]<\/code>, which is the string <code>\"ABCD\"<\/code>, to the function <em>fourchar()<\/em>. Here&#8217;s the code that does that:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid fourchar(char p[])\r\n{\r\n    puts(p);\r\n}\r\n\r\nint main()\r\n{\r\n    char text[2][5] = { \"ABCD\", \"WXYZ\" };\r\n\r\n    fourchar(text[0]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The argument for <em>fourchar()<\/em> is in the form <code>array[<em>n<\/em>]<\/code> where <em>n<\/em> is an element number. The element number is omitted in <em>fourchar()<\/em>&#8216;s declaration. And in the function itself, the variable name <code>p<\/code> is used, but not the square brackets. This approach has some quirks, but my guess is that most pointer-fearful programmers prefer it for sending a string to a function.<\/p>\n<p>But.<\/p>\n<p>Suppose the purpose of the <em>fourchar()<\/em> function is to process the two-dimensional array one element at a time? In that setup, you&#8217;d still use this statement to pass the string to the function:<\/p>\n<p><code>fourchar(text[0]);<\/code><\/p>\n<p>The variable <code>text[0]<\/code> is equivalent to the first character in the string, <code>text[0][0]<\/code>. So far, so good. The trouble lies in the <em>fourchar()<\/em> function. You cannot do this declaration:<\/p>\n<p><code>void fourchar(char p[][]);<\/code><\/p>\n<p>The compiler has no clue what a <code>p[][]<\/code> is. Most programmers would edit the declaration back to:<\/p>\n<p><code>void fourchar(char p[]);<\/code><\/p>\n<p>Within the function you could try using the <code>p[][]<\/code> variable, but again the compiler would balk. In fact, you can&#8217;t even use <code>p[]<\/code> within the function: Only variable <code>p<\/code> represents the string passed to the <em>fourchar()<\/em> function; variable <code>p[]<\/code> is unknown.<\/p>\n<p>The best way to process a string character-by-character in this manner is to use the <code>**<\/code> type of pointer, which is quite similar to what the <code>text[][]<\/code> notation represents.<\/p>\n<p>I&#8217;ll show that solution in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2350\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The relationship between <code>**ptr<\/code> and <code>array[][]<\/code>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2329\">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-2329","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\/2329","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=2329"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2329\/revisions"}],"predecessor-version":[{"id":2378,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2329\/revisions\/2378"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}