{"id":5739,"date":"2023-02-11T00:01:38","date_gmt":"2023-02-11T08:01:38","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5739"},"modified":"2023-02-04T10:57:34","modified_gmt":"2023-02-04T18:57:34","slug":"concatenating-arrays","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5739","title":{"rendered":"Concatenating Arrays"},"content":{"rendered":"<p>Yet another tool missing from C, but found in other programming languages, is the capability to stick together two arrays. The result is a single, larger array containing the elements from the two originals. With C, of course, you can always code your own function to handle the task.<br \/>\n<!--more--><br \/>\nThe <em>strcat()<\/em> function does array concatenation, gluing together two character arrays &mdash; but only character arrays (strings). Beyond strings, an array concatenation function must know the array&#8217;s data type. This tidbit requires either separate, data type specific functions or that the data type be passed as an argument to a generic array concatenation function.<\/p>\n<p>Another required knowledge tidbit is the array&#8217;s element count. A string is terminated with the null character, but any other type of array has no guaranteed final element value. As the arrays are referenced by address (pointer) within the function, you can&#8217;t use the <em>sizeof<\/em> keyword to determine the number of elements: In a function, <em>sizeof<\/em> returns the pointer&#8217;s size, not the array size.<\/p>\n<p>Here is my <em>intarraycat()<\/em> function, which concatenates two integer arrays:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* concatenate two integer arrays *\/<\/span>\r\nint *intarraycat(int *a, int asize, int *b, int bsize)\r\n{\r\n    int *c,x;\r\n\r\n    <span class=\"comments\">\/* allocate storage for new array *\/<\/span>\r\n    c = malloc( sizeof(int) * (asize+bsize) );\r\n\r\n    <span class=\"comments\">\/* concatenate upon success *\/<\/span>\r\n    if( c!=NULL )\r\n    {\r\n        <span class=\"comments\">\/* copy first array *\/<\/span>\r\n        for( x=0; x&lt;asize; x++ )\r\n            *(c+x) = *(a+x);\r\n        <span class=\"comments\">\/* copy second array *\/<\/span>\r\n        for( x=0; x&lt;bsize; x++ )\r\n            *(c+x+asize) = *(b+x);\r\n    }\r\n\r\n    return(c);\r\n}<\/pre>\n<p>The four arguments are two pairs, the arrays and their number of elements. Variable <code>c<\/code> is allocated to store both array&#8217;s elements, using the two size values to set the storage size. Upon success, the <em>if<\/em> statement block executes. Otherwise, <code>NULL<\/code> is returned, which is tested for in the <em>main()<\/em> function.<\/p>\n<p>Two <em>for<\/em> loops in the function assign array elements. Pointer notation is used as the array data is passed as a pointer and referenced in variable <code>c<\/code>.<\/p>\n<p>In the second <em>for<\/em> loop, the offset for buffer <code>c<\/code> is calculated by using both looping variable <code>x<\/code> and the size of the first array, <code>asize<\/code>. This expression ensures that the elements in new array <code>c<\/code> are contiguous.<\/p>\n<p>The function returns pointer <code>c<\/code>, which references the buffer containing the combined arrays.<\/p>\n<p>The <em>main()<\/em> function declares two integer arrays and assigns them values, outputs each array, then calls the <em>intarraycat()<\/em> function in these statements:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* concatenate and output *\/<\/span>\r\n    cat = intarraycat(a,as,b,bs);\r\n    if( cat!=NULL )\r\n    {\r\n        printf(\"Result: \");\r\n        for( x=0; x&lt;as+bs; x++ )\r\n            printf(\" %d\",*(cat+x));\r\n        putchar('\\n');\r\n    }\r\n    else\r\n    {\r\n        puts(\"Concatenation error\");\r\n    }<\/pre>\n<p>Integer pointer <em>cat<\/em> captures the return value from the <em>intarraycat()<\/em> function. If the value is not <code>NULL<\/code>, a <em>for<\/em> loop outputs the new concatenated array. Otherwise, an error message is output. Variables <code>a<\/code> and <code>b<\/code> represent the two original integer arrays; <code>as<\/code> and <code>bs<\/code> are the array sizes. <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_02_11-Lesson.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the full code. Here is sample output:<\/p>\n<p><code>Array 'a': 11 22 33 44 55<br \/>\nArray 'b': 89 78 67 56 45 1<br \/>\nResult:  11 22 33 44 55 89 78 67 56 45 1<\/code><\/p>\n<p>I suppose a more graceful way to concatenate two arrays might exist. Then again, in other programming languages, the coders need not worry about elegant coding because such a function already exists. In C, we must try harder.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array A plus Array B equals new Array C, but you must code your own function to do it. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5739\">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-5739","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\/5739","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=5739"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5739\/revisions"}],"predecessor-version":[{"id":5750,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5739\/revisions\/5750"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}