{"id":1699,"date":"2016-01-02T00:01:27","date_gmt":"2016-01-02T08:01:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1699"},"modified":"2016-01-10T10:02:20","modified_gmt":"2016-01-10T18:02:20","slug":"your-own-strdup-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1699","title":{"rendered":"Your Own <em>strdup()<\/em> Function"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1690\">last week&#8217;s Lesson<\/a>, I demonstrated the <em>strdup()<\/em> function. That function isn&#8217;t available in every C language library, but it&#8217;s easy to craft your own.<br \/>\n<!--more--><br \/>\nAs a review, the <em>strdup()<\/em> function does two things:<\/p>\n<p>1. Allocates space for a new string, setting the size the same as the original string<br \/>\n2. Copies the original string into the new allocated space, including the null character (<code>\\0<\/code>)<\/p>\n<p>If space can&#8217;t be allocated, the function returns a <code>NULL<\/code> pointer.<\/p>\n<p>I called my faux <em>strdup()<\/em> function <em>dupstring()<\/em>, which is defined in the following code, a modification of the example from last week&#8217;s Lesson:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nchar *dupstring(char *org)\r\n{\r\n    int org_size;\r\n    static char *dup;\r\n    char *dup_offset;\r\n\r\n    <span class=\"comments\">\/* Allocate memory for duplicate *\/<\/span>\r\n    org_size = strlen(org);\r\n    dup = (char *)malloc(sizeof(char)*org_size+1);\r\n    if( dup == NULL)\r\n        return( (char *)NULL);\r\n\r\n    <span class=\"comments\">\/* Copy string *\/<\/span>\r\n    dup_offset = dup;\r\n    while(*org)\r\n    {\r\n        *dup_offset = *org;\r\n        dup_offset++;\r\n        org++;\r\n    }\r\n    *dup_offset = '\\0';\r\n\r\n    return(dup);\r\n}\r\n\r\nint main()\r\n{\r\n    char original[12] = \"Ooga Booga!\";\r\n    char *duplicate;\r\n    int o_len,d_len;\r\n\r\n    duplicate = dupstring(original);\r\n    o_len = strlen(original);\r\n    d_len = strlen(duplicate);\r\n\r\n    printf(\"Original String: '%s' (%d)\\n\",\r\n            original,o_len);\r\n    printf(\"Duplicate string: '%s' (%d)\\n\",\r\n            duplicate,d_len);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>dupstring()<\/em> function is declared at Line 5. The string created is held in the <code>*dup<\/code> pointer, which is made <em>static<\/em> at Line 8. It must be <em>static<\/em>, lest the string be lost when the function terminates.<\/p>\n<p>The first step takes place at Lines 11 through 15, where storage for the duplicate string is allocated. The original string&#8217;s size is fetched (Line 12) and then the <em>malloc()<\/em> function allocates space for the duplicate, plus one character for the <code>\\0<\/code> (Line 13). Don&#8217;t forget that <code>+1<\/code>! The <em>strlen()<\/em> function doesn&#8217;t count the null character.<\/p>\n<p>If the allocation fails, the <code>NULL<\/code> pointer is returned (Line 14 and 15).<\/p>\n<p>After space is allocated, a <em>while<\/em> loop copies the original string into the duplicate. I use the <code>dup_offset<\/code> pointer so that the address of <code>dup<\/code> (the duplicate string) is retained.<\/p>\n<p>Because the <em>while<\/em> loop doesn&#8217;t copy the null character, that character is appended to the duplicate string at Line 25. Then the duplicate string is returned.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Original String: 'Ooga Booga!' (11)\r\nDuplicate string: 'Ooga Booga!' (11)<\/code><\/pre>\n<p>Remember that the C language is written in the C language. Any function you desire, you can code. Even the <em>strndup()<\/em> function, which reads only the first <em>n<\/em> character of a string. Try it on your own!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Allocate space for and duplicate a string. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1699\">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-1699","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\/1699","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=1699"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1699\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1699\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}