Of course, there are ways - just nothing is built in. Many C utility libraries have functions for this β for example, glib g_strjoinv . You can also flip your own, for example:
static char *util_cat(char *dest, char *end, const char *str) { while (dest < end && *str) *dest++ = *str++; return dest; } size_t join_str(char *out_string, size_t out_bufsz, const char *delim, char **chararr) { char *ptr = out_string; char *strend = out_string + out_bufsz; while (ptr < strend && *chararr) { ptr = util_cat(ptr, strend, *chararr); chararr++; if (*chararr) ptr = util_cat(ptr, strend, delim); } return ptr - out_string; }
The main reason it is not built-in is because the standard C library is very minimal; they wanted to simplify the creation of new C implementations, so you wonβt find so many useful features. There is also a problem that C does not give you many recommendations on how, for example, to decide how many elements are in arrays (I used the terminator convention of NULL array elements in the above example).
source share