{"id":4816,"date":"2022-08-24T16:38:20","date_gmt":"2022-08-24T11:08:20","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/"},"modified":"2022-08-24T16:38:20","modified_gmt":"2022-08-24T11:08:20","slug":"solved-does-this-function-i-made-correctly-append-a-string-to-another-string","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/","title":{"rendered":"[Solved] Does this function I made correctly append a string to another string?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-43422468\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"43422468\" data-parentid=\"43422030\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>First, <code>char *stuff_to_append_to[]<\/code> is an <em>array of pointers of undetermined length<\/em>, that is not a valid parameter, as the last dimension of an array must be specified when passed to a function, otherwise, pass a pointer to type.<\/p>\n<p>Next, <code>char **stuff_to_append<\/code> is a <em>pointer to pointer to char<\/em> and is completely valid, but given your use of <code>stuff_to_append<\/code> within the function, it is obvious that is not what you intended.<\/p>\n<p>If you wish to <strong>insert<\/strong> <code>stuff_to_append<\/code> and <strong>truncate<\/strong> <code>stuff_to_append_to<\/code> at the end of <code>stuff_to_append<\/code> simply pass a pointer to each string as the argument. While <code>int position<\/code> is fine, a choice of an <em>unsigned<\/em> value may be better as you will not insert at a <em>negative<\/em> array index.<\/p>\n<p>Inside your function, you must validate there is sufficient space in <code>stuff_to_append_to<\/code> to hold <code>stuff_to_append<\/code> beginning at index <code>position<\/code> (including space for the <em>nul-byte<\/em>)<\/p>\n<p>With that in mind, you may want to do something like the following:<\/p>\n<pre><code>void append_this_stuff (char *stuff_to_append_to, char *stuff_to_append, \n                        int position) \n{\n    int somenumber = strlen (stuff_to_append),\n        lento = strlen (stuff_to_append_to),\n        end = position + somenumber;\n\n    if (end &gt; lento) {\n        fprintf (stderr, \"error: insufficient space in stuff_to_append_to.\\n\");\n        return;\n    }\n\n    for (int i = position; i &lt; end + 1; i++)    \/* +1 to force copy of nul-byte *\/\n        stuff_to_append_to[i] = stuff_to_append[i - position];\n}\n<\/code><\/pre>\n<p>You can write a small test program to confirm it&#8217;s operation, e.g.<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n...\nint main (void) {\n\n    char stuff[] = \"my dog has fleas!\",\n        append[] = \"cat has none!\";\n    int pos = 3;\n\n    printf (\"original: %s\\n\", stuff);\n    append_this_stuff (stuff, append, pos);\n    printf (\"     new: %s\\n\", stuff);\n\n    return 0;\n}\n<\/code><\/pre>\n<p><strong>Example Use\/Output<\/strong><\/p>\n<pre><code>$ .\/bin\/append\noriginal: my dog has fleas!\n     new: my cat has none!\n<\/code><\/pre>\n<p>To do the same thing using <em>pointer arithmetic<\/em> instead of using <em>array indexes<\/em>, you can rewrite <code>append_this_stuff<\/code> similar to the following:<\/p>\n<pre><code>void ats (char *to, char *from, int p)\n{\n    if (p + strlen (from) &gt; strlen (to)) {\n        fprintf (stderr, \"error: insufficient space in stuff_to_append_to.\\n\");\n        return;\n    }\n\n    for (to += p; *from; to++, from++)\n        *to = *from;\n    *to = *from;\n}\n<\/code><\/pre>\n<p>Lastly, if this lesson is not completely ingrained in your thought process, &#8220;<em>Never post anything that you would not want in the hands of the recruiter as you interview for your first programming position.<\/em>&#8221; The use of unprofessional or cute variable names, while it may express your frustrations, may not give the impression you want in the hands of another. Enough said.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Does this function I made correctly append a string to another string? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] First, char *stuff_to_append_to[] is an array of pointers of undetermined length, that is not a valid parameter, as the last dimension of an array must be specified when passed to a function, otherwise, pass a pointer to type. Next, char **stuff_to_append is a pointer to pointer to char and is completely valid, but given &#8230; <a title=\"[Solved] Does this function I made correctly append a string to another string?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\" aria-label=\"More on [Solved] Does this function I made correctly append a string to another string?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,1093,472,1092,362],"class_list":["post-4816","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-c99","tag-char","tag-concatenation","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Does this function I made correctly append a string to another string? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Does this function I made correctly append a string to another string? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] First, char *stuff_to_append_to[] is an array of pointers of undetermined length, that is not a valid parameter, as the last dimension of an array must be specified when passed to a function, otherwise, pass a pointer to type. Next, char **stuff_to_append is a pointer to pointer to char and is completely valid, but given ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-24T11:08:20+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Does this function I made correctly append a string to another string?\",\"datePublished\":\"2022-08-24T11:08:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\"},\"wordCount\":269,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"c99\",\"char\",\"concatenation\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\",\"name\":\"[Solved] Does this function I made correctly append a string to another string? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-24T11:08:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Does this function I made correctly append a string to another string?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Does this function I made correctly append a string to another string? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Does this function I made correctly append a string to another string? - JassWeb","og_description":"[ad_1] First, char *stuff_to_append_to[] is an array of pointers of undetermined length, that is not a valid parameter, as the last dimension of an array must be specified when passed to a function, otherwise, pass a pointer to type. Next, char **stuff_to_append is a pointer to pointer to char and is completely valid, but given ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/","og_site_name":"JassWeb","article_published_time":"2022-08-24T11:08:20+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Does this function I made correctly append a string to another string?","datePublished":"2022-08-24T11:08:20+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/"},"wordCount":269,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","c99","char","concatenation","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/","url":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/","name":"[Solved] Does this function I made correctly append a string to another string? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-24T11:08:20+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-does-this-function-i-made-correctly-append-a-string-to-another-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Does this function I made correctly append a string to another string?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=4816"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4816\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}