There is no standard library function to do that.
The most efficient solution is to compute the size of the concatenated string, and then strcpy the components into the correct place. That requires keeping track of the sizes of the strings, which is a bit of overhead, or scanning the length of each component string twice.
Using strcat will require rescanning the constructed string on every concatenation, leading to quadratic running time. However, if you know you won't have too many strings in the array and the operation is not in a critical loop, it might be acceptable.
Here's a simple recursive solution, for demonstration purposes. Like all recursive programs, it should be used with caution in production code since it has the potential to overflow the call stack, but it can easily be modified to become an iterative solution.
(The standard iterative transformation would require a stack to retain the state of the recursive function, but the part of the state required after the recursive call consists only of my_len which could be recomputed.)
char* join_helper(char** in, size_t inlen, size_t inpos, size_t accum) {
if (inpos == inlen)
return strcpy(malloc(accum + 1) + accum, "");
else {
size_t mylen = strlen(in[inpos]);
return memcpy(
join_helper(in, inlen, inpos + 1, accum + mylen) - mylen,
in[inpos], mylen);
}
}
/* The string returned by this function is malloc'd and needs
* to be free'd by the caller
*/
char* join(char** in, size_t inlen) {
return join_helper(in, inlen, 0, 0);
}
asprintf(3)function that allocates the target string for you that you could have used withasprintf(&concat, "%s%s%s", s[0], s[1], s[2])to save a slight amount of work, but it's non-standard. :)