{"id":7166,"date":"2025-09-27T00:01:30","date_gmt":"2025-09-27T07:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7166"},"modified":"2025-10-04T08:40:49","modified_gmt":"2025-10-04T15:40:49","slug":"copying-strings","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7166","title":{"rendered":"Copying Strings"},"content":{"rendered":"<p>The C language has string manipulation functions, but they&#8217;re tepid. In fact, the C23 update includes <em>even more<\/em> functions to duplicate strings and to stick them together. These actions are simple in other languages, but create such a problem for C that coding a string copying function is often a skill you must demonstrate when applying for a C programming job.<br \/>\n<!--more--><br \/>\nFor example, in PHP you use the obvious <code>+<\/code> operator to stick two strings together:<\/p>\n<p><code>$string = \"this\" + \"that\";<\/code><\/p>\n<p>Such an expression in C would make the compiler barf. No, you&#8217;re instructed as a beginning C programmer to use a function to stick two strings together. Worse, you&#8217;re forced to learn the word <em>concatenation<\/em>, which is the single-word, 5-sylable monster that means &#8220;to stick two things together.&#8221; (Well, not exactly, but the real answer isn&#8217;t funny.)<\/p>\n<p>Consider the following code, which shows a typical example of how a string is copied from one buffer into another:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_27-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_09_27-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char dest[16];\r\n    char src[] = \"The string is the thing\";\r\n\r\n    strcpy(dest,src);\r\n\r\n    printf(\"%s\\n\",dest);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Array <code>dest[]<\/code> holds 16 bytes. Array <code>src[]<\/code> is a string, but it&#8217;s 23 characters long &mdash; too big to fix into the destination string. So where do those extra characters go?<\/p>\n<p>No one knows.<\/p>\n<p>Is this bad?<\/p>\n<p>Yes, it is. Buffer overflow is a security risk. Further, the build process doesn&#8217;t catch this flaw. Running the program may result in a segmentation fault, but my purpose is to demonstrate how weak the <em>strcpy()<\/em> function is &mdash; and it&#8217;s used all the time.<\/p>\n<p>This modification to the code definitely results in a segmentation fault\/core dump on my machine:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_27-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_09_27-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char dest[11];\r\n    char src[] = \"The string is the thing\";\r\n\r\n    strcpy(dest,src);\r\n\r\n    printf(\"Copied '%s' to '%s'\\n\",src,dest);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>I changed the buffer size for <code>dest[]<\/code> to 11 bytes. The original size of 16 bytes may cause the buffer&#8217;s memory alignment to provide enough space to accommodate the overflow. A value of 11 is weird, which means the next item located in memory can be clobbered by the incoming text and generate a segmentation fault.<\/p>\n<p>This final example shows a string being copied into an occupied buffer:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_09_27-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2025_09_27-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char dest[] = \"Don't clobber me!\";\r\n    char src[] = \"String thing\";\r\n\r\n    strcpy(dest,src);\r\n\r\n    printf(\"Copied '%s' to '%s'\\n\",src,dest);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In this code, <code>dest[]<\/code> is longer than <code>src[]<\/code>, so enough room is available. But no checking is done to ensure that the buffer isn&#8217;t occupied &mdash; not that this condition is a concern, but should a test be made? Could it be made? And what happens to the leftover characters in the original buffer? It&#8217;s lost storage.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7182\">next week&#8217;s Lesson<\/a>, I offer a custom string copying function, similar to the one that a potential employer may ask C programmer to code. If you like, you can try to code your own &#8220;better&#8221; version of <em>strcpy()<\/em> before then. After all, if you&#8217;re planning on getting a coding job, it might just be good practice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting a string from one buffer into another seems simple, but can have issues. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7166\">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-7166","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\/7166","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=7166"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7166\/revisions"}],"predecessor-version":[{"id":7197,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7166\/revisions\/7197"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}