{"id":7137,"date":"2025-09-06T00:01:33","date_gmt":"2025-09-06T07:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7137"},"modified":"2025-09-13T09:54:58","modified_gmt":"2025-09-13T16:54:58","slug":"the-trouble-with-array-decay","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7137","title":{"rendered":"The Trouble with Array Decay"},"content":{"rendered":"<p>Array decay has nothing to do with too much sugar in your code or poor brushing habits. No, it&#8217;s a hot topic in C programming, and a source of confusion.<br \/>\n<!--more--><br \/>\nArray decay is a byproduct of the tenuous relationship between arrays and pointers in the C language. It might be the reason why some programmers avoid pointers because the two are similar &mdash; to a degree.<\/p>\n<p>&#128504; Arrays are not pointers.<\/p>\n<p>&#128504; Pointers are not arrays.<\/p>\n<p>A connection exists between pointers and arrays, however, which is what array decay describes. The first element of an array, truly the array name itself, represents an address. The behavior is shown in the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_06-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_09_06-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char str1[] = \"I am string one!\";\r\n    char *str2 = \"I am string two!\";\r\n\r\n    puts(str1);\r\n    puts(str2);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Variable <code>str1[]<\/code> is a character array, a string. So is variable <code>str2<\/code>, though it&#8217;s declared as a pointer. Strings are declared either way in C. With <code>str1[]<\/code>, you use array notation to access the individual characters (elements). With <code>str2<\/code>, pointer notation is used. (Also, <code>str2<\/code> has its own quirks, which I discuss <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3475\">here<\/a>.)<\/p>\n<p>Both variables are used in the same manner with the <em>puts()<\/em> function to output the strings:<\/p>\n<p><code>I am string one!<br \/>\nI am string two!<\/code><\/p>\n<p>The compiler sees both <code>str1[]<\/code> and <code>str2<\/code> as addresses, the location in memory where a string is stored. This effect is how array decay works: The first element in an array &#8220;decays&#8221; to a pointer. The element isn&#8217;t specified directly. If you try the statement <code>puts(str1[0]<\/code>), the compiler generates a warning as <code>str1[0]<\/code> is a single character and not a string. Running the program results in Bad Things happening.<\/p>\n<p>The following code also shows array decay in action, where the array is assigned to a pointer:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_06-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_09_06-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int array[] = { 10, 20, 30, 40 };\r\n    int *ptr;\r\n\r\n    ptr = array;\r\n    printf(\"%d\\n\",*ptr);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Due to the array decay, the statement <code>ptr = array<\/code> works; the array&#8217;s base address is saved in <em>int<\/em> pointer <code>ptr<\/code>. Further, the pointer is used in the <em>printf()<\/em> statement to output the value of the array&#8217;s first element, 10.<\/p>\n<p>The code could further use the pointer to reference other elements in the array. For example, <code>*(ptr+3)<\/code> references the fourth array element, 40.<\/p>\n<p>This witchery doesn&#8217;t work in reverse. You cannot use an array to dig into a pointer&#8217;s buffer. After all, the effect is called &#8220;array decay&#8221; and not &#8220;whacky pointer things.&#8221;<\/p>\n<p>Where array decay comes in handy is when dealing with arrays and functions. I explore this aspect of array decay in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7153\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you discussed array decay with your Dentist? Because if you have, you&#8217;re wasting your time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7137\">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-7137","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\/7137","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=7137"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7137\/revisions"}],"predecessor-version":[{"id":7164,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7137\/revisions\/7164"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}