{"id":2350,"date":"2017-02-11T00:01:45","date_gmt":"2017-02-11T08:01:45","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2350"},"modified":"2017-02-04T08:33:26","modified_gmt":"2017-02-04T16:33:26","slug":"manipulate-pointers-in-functions-part-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2350","title":{"rendered":"Manipulate Pointers in Functions, Part V"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2306\">Part II of this series<\/a>, I showed code that let you pass a pointer&#8217;s address to a function. Within that function, the address can be manipulated and value at that address displayed. Can the same thing be done with a two-dimensional array, which is loosely related to a <code>**<\/code> pointer?<br \/>\n<!--more--><br \/>\nAs a reference, here is the <em>fourchar()<\/em> function I used in Part II&#8217;s sample code:<\/p>\n<pre class=\"screen\">\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}<\/pre>\n<p>For a second, forget pointers and pointers-to-pointers. Consider instead an array.<\/p>\n<p>A single dimension array is like a pointer&#8217;s cousin. Lots of C programmers rely on the similarity to avoid using pointers. In a way, a two-dimensional array is like a pointer-pointer. The problem with this similarity is that it doesn&#8217;t really translate well.<\/p>\n<p>For example, assume that you have a two-dimensional array, one that stores two strings:<\/p>\n<p><code>char text[2][5] = { \"ABCD\", \"WXYZ\" };<\/code><\/p>\n<p>You want to send strings from this array to the <em>fourchar()<\/em> function. So you write the following function:<\/p>\n<pre class=\"screen\">\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}<\/pre>\n<p>This code won&#8217;t compile. The compiler has no idea what the argument <code>p[][]<\/code> is. Yet, your code needs to manipulate a <em>char<\/em> array in the manner shown above. The function can take <code>p[]<\/code> as an argument, but within the function, how can you access individual array elements? One solution is to use a pointer-pointer instead.<\/p>\n<p>The following code is similar to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2329\">last week&#8217;s Lesson<\/a> code, but uses pointers instead of array notation.<\/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    char c;\r\n\r\n    for(x=0;x&lt;4;x++)\r\n    {\r\n        c = *(*p+x);\r\n        putchar(c);\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char *text[] = { \"ABCD\", \"WXYZ\" };\r\n\r\n    fourchar(&amp;text[0]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the <em>main()<\/em> function, <code>*text[]<\/code> is an array. You could declare it as a two-dimensional array, but then you lock yourself out of the option of sending a <code>text[][]<\/code> item to a function. So you must declare an array of <em>char<\/em> pointers to make the function call work. (You could also declare the variable as <code>**text<\/code>, but that would involve using <em>malloc()<\/em> and assigning strings and other nonsense.)<\/p>\n<p>At Line 19, the <code>fourchar()<\/code> function is called with the address of the first string in the <code>*text[]<\/code> array.<\/p>\n<p>In the <em>fourchar()<\/em> function, variable <code>**p<\/code> represents the address of a pointer, which is the address of the string. A <em>for<\/em> loop marches through each character in the string. Variable <code>c<\/code> holds the character fetched. It&#8217;s not a required variable, but it does eliminate a layer of parentheses that would otherwise cluster inside the <em>putchar()<\/em> function. Here&#8217;s how Line 10 unwinds:<\/p>\n<p><code>c = *(*p+x);<\/code><\/p>\n<p>At the core, <code>*p<\/code> represents an address, the address of string <code>\"ABCD\"<\/code>. Variable <code>x<\/code> is an offset from that address. You must format it as <code>*p+x<\/code>, which evaluates as an address. To fetch the character at that address, you enclose <code>*p+x<\/code> in parentheses and use the asterisk: <code>*(*p+x)<\/code>.<\/p>\n<p>In the end, the code marches through each character in the array, just as if the <code>p[][]<\/code> format were used.<\/p>\n<p>If you like, you can add a second statement to the <em>main()<\/em> function:<\/p>\n<p><code>fourchar(&text[1]);<\/code><\/p>\n<p>This statement processes the second string in the <code>*text[]<\/code> array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get that two-dimensional array into a function and manipulate its second element, all by using pointers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2350\">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-2350","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\/2350","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=2350"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2350\/revisions"}],"predecessor-version":[{"id":2372,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2350\/revisions\/2372"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}