{"id":15584,"date":"2022-10-12T05:10:03","date_gmt":"2022-10-11T23:40:03","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/"},"modified":"2022-10-12T05:10:03","modified_gmt":"2022-10-11T23:40:03","slug":"solved-how-to-choose-types-for-a-function-prototype","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/","title":{"rendered":"[Solved] How to choose types for a function prototype?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-36959102\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"36959102\" data-parentid=\"36958740\" data-score=\"3\" 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>I&#8217;m going to suppose that you are trying to make a deep copy of the <code>argv<\/code> array, with that being a <code>NULL<\/code>-terminated array of strings such as the second parameter of a C program&#8217;s <code>main()<\/code> function.  The function you present seems to assume that you have already allocated space for the destination array itself; its job seems limited to copying the argument strings.<\/p>\n<p>First things first, then: let&#8217;s look at the caller.  If you&#8217;re making a deep copy of a standard argument vector, then the type of the destination variable should be compatible with the type of <code>argv<\/code> itself (in the colloquial sense of &#8220;compatible&#8221;).  If the lifetime of the copy does not need to extend past the host function&#8217;s return, then a variable-length array would be a fine choice:<\/p>\n<pre><code>char *copy[argc + 1];\n<\/code><\/pre>\n<p>That relieves you of manually managing the memory of the array itself, but not of managing any memory uniquely allocated to its elements.  On the other hand, if you need the copy to survive return from the function in which it is declared, then you&#8217;ll have to use manual allocation:<\/p>\n<pre><code>char **copy = malloc((argc + 1) * sizeof(*copy));\nif (!copy) \/* handle allocation failure *\/ ;\n<\/code><\/pre>\n<p>Either way, you can pass the resulting array or pointer itself to your <code>write_command()<\/code> function, and the required parameter type is the same.  It is pointless to pass a pointer to <code>copy<\/code>, because that function will not modify the pointer it receives as its argument; rather, it will modify the memory to which it points.<\/p>\n<p>Here is the signature of the function you seem to want:<\/p>\n<pre><code>void write_command(char *argv[], char *string[]) {\n<\/code><\/pre>\n<p>Given such a signature, you would call it as &#8230;<\/p>\n<pre><code>write_command(argv, copy);\n<\/code><\/pre>\n<p>&#8230;.<\/p>\n<p>The key step you seem to want to perform in the loop inside is<\/p>\n<pre><code>    string[r] = strdup(argv[r]);\n<\/code><\/pre>\n<p>You can accomplish the same thing with a <code>malloc()<\/code>, initialize, <code>strcpy()<\/code> sequence, but that&#8217;s a bit silly when <code>stdrup()<\/code> is ready-made for the same task.  Do not forget to check its return value, however, (or in your original code, the return value of <code>malloc()<\/code>) in case memory allocation fails.  Any way around, you  must <em>not<\/em> free the allocated memory within <code>write_command()<\/code>, because that leaves you with invalid pointers inside your copied array.<\/p>\n<hr>\n<p>Furthermore, even if you really do have a 2D array of <code>char *<\/code> in the caller, such as &#8230;<\/p>\n<pre><code>char *copies[n][argc + 1];\n<\/code><\/pre>\n<p>&#8230; <em>nothing changes<\/em> with function <code>write_command()<\/code>.  It doesn&#8217;t need to know or care whether the array it&#8217;s copying into is an element of a 2D array.  You simply have to call it appropriately, something like:<\/p>\n<pre><code>write_command(argv, copies[w]);\n<\/code><\/pre>\n<hr>\n<p>No matter what, you must be sure to free the copied argument strings, but only after you no longer need them.  Again, you cannot do that inside the <code>write_command()<\/code> function.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to choose types for a function prototype? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;m going to suppose that you are trying to make a deep copy of the argv array, with that being a NULL-terminated array of strings such as the second parameter of a C program&#8217;s main() function. The function you present seems to assume that you have already allocated space for the destination array itself; &#8230; <a title=\"[Solved] How to choose types for a function prototype?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\" aria-label=\"More on [Solved] How to choose types for a function prototype?\">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,413,2347],"class_list":["post-15584","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-function","tag-refactoring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to choose types for a function prototype? - 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-how-to-choose-types-for-a-function-prototype\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to choose types for a function prototype? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;m going to suppose that you are trying to make a deep copy of the argv array, with that being a NULL-terminated array of strings such as the second parameter of a C program&#8217;s main() function. The function you present seems to assume that you have already allocated space for the destination array itself; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-11T23:40:03+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-how-to-choose-types-for-a-function-prototype\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to choose types for a function prototype?\",\"datePublished\":\"2022-10-11T23:40:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\"},\"wordCount\":438,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"function\",\"refactoring\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\",\"name\":\"[Solved] How to choose types for a function prototype? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-11T23:40:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to choose types for a function prototype?\"}]},{\"@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=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to choose types for a function prototype? - 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-how-to-choose-types-for-a-function-prototype\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to choose types for a function prototype? - JassWeb","og_description":"[ad_1] I&#8217;m going to suppose that you are trying to make a deep copy of the argv array, with that being a NULL-terminated array of strings such as the second parameter of a C program&#8217;s main() function. The function you present seems to assume that you have already allocated space for the destination array itself; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/","og_site_name":"JassWeb","article_published_time":"2022-10-11T23:40:03+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-how-to-choose-types-for-a-function-prototype\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to choose types for a function prototype?","datePublished":"2022-10-11T23:40:03+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/"},"wordCount":438,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","function","refactoring"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/","name":"[Solved] How to choose types for a function prototype? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-11T23:40:03+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-choose-types-for-a-function-prototype\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to choose types for a function prototype?"}]},{"@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=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","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\/15584","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=15584"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15584\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=15584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=15584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=15584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}