{"id":7153,"date":"2025-09-13T00:01:43","date_gmt":"2025-09-13T07:01:43","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7153"},"modified":"2025-09-06T14:13:34","modified_gmt":"2025-09-06T21:13:34","slug":"more-array-more-decay","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7153","title":{"rendered":"More Array, More Decay"},"content":{"rendered":"<p>I&#8217;ll be blunt: If you want to pass an array to a function, or have a function return an array, just give up now and use pointers. It&#8217;s a far easier operation, and it would save me the bother of having to write this post to further discuss the topic of array decay.<br \/>\n<!--more--><br \/>\nPicking up from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7137\">last week&#8217;s Lesson<\/a>, array decay can be most notable when working with a function. In the following code, an array is passed to a function, then output as an array within the function:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_13-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_09_13-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid output( int a[] )\r\n{\r\n    for( int x=0; x&lt;4; x++ )\r\n        printf(\"%d\\n\",a[x]);\r\n}\r\n\r\nint main()\r\n{\r\n    int array[] = { 10, 20, 30, 40 };\r\n\r\n    output(array);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>From the <em>main()<\/em> function, <code>array[]<\/code> is passed to function <em>output(<\/em>). This function declares the array as its sole argument, <code>a[]<\/code>. Array notation is used within the function to output each element&#8217;s value:<\/p>\n<p><code>10<br \/>\n20<br \/>\n30<br \/>\n40<\/code><\/p>\n<p>Here is the same code, but using pointer notation in function <em>output()<\/em>:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_13-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_09_13-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid output( int a[] )\r\n{\r\n    for( int x=0; x&lt;4; x++ )\r\n        printf(\"%d\\n\",*(a+x) );\r\n}\r\n\r\nint main()\r\n{\r\n    int array[] = { 10, 20, 30, 40 };\r\n\r\n    output(array);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>printf(<\/em>) statement uses pointer notation to output array <code>a[]<\/code>&#8216;s elements. This switch works because the array is passed by name as an address. In fact, you could declare the <em>output()<\/em> function&#8217;s argument as <code>int *a<\/code> and the code still works &mdash; though you can&#8217;t use array notation in the function when this switch is made.<\/p>\n<p>One of the more technical aspects of array decay is that is removes the code&#8217;s capability to determine the array&#8217;s size. Consider this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_13-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2025_09_13-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nchar *title(void)\r\n{\r\n    static char t[] = \"Fun Array Stuff\";\r\n\r\n    printf(\"'%s' contains %zu bytes\\n\",\r\n            t,\r\n            sizeof(t)\r\n          );\r\n\r\n    return(t);\r\n}\r\n\r\nint main()\r\n{\r\n    printf(\"'%s' contains %zu bytes\\n\",\r\n            title(),\r\n            sizeof( title() )\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<p><code>'Fun Array Stuff' contains 16 bytes<br \/>\n'Fun Array Stuff' contains 8 bytes<\/code><\/p>\n<p>In the <em>title()<\/em> function, array <code>t[]<\/code> must be declared <em>static<\/em> or its data is lost and cannot be returned. Within the function, the <em>sizeof<\/em> operator reports the number of bytes that the string occupies in storage.<\/p>\n<p>Back in the <em>main(<\/em>) function, <em>title()<\/em> returns the string &mdash; its address, as the function is a <em>char<\/em> pointer. But now, the <em>sizeof<\/em> operator reports the number of bytes that the <em>address<\/em> (the pointer) occupies in memory. The string&#8217;s size is lost due to array decay. (Yes, the <em>strlen()<\/em> function  still works, but array decay has transformed the data.)<\/p>\n<p>Some might say that such an issue isn&#8217;t a problem. Even so, it may come as a surprise when you expect to return an array from a function but it&#8217;s form is lost. I don&#8217;t find array decay an issue in my code because I allocate storage and use pointers when I can. But one area where it might be a problem is with an array of structures. I can&#8217;t think of a good example to mess with at this point, though it seems like an interesting problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array decay is most evident when it comes to using arrays with functions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7153\">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-7153","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\/7153","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=7153"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7153\/revisions"}],"predecessor-version":[{"id":7160,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7153\/revisions\/7160"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}