Walt
Walt

Reputation: 1531

Is there an easy way to create a string from a string array in C?

I am talking about some sort of function that can combine an array of character arrays into a single string.

Would I just have to loop through the array of strings and do it manually? The string was created using strtok().

Upvotes: 0

Views: 93

Answers (2)

Philip
Philip

Reputation: 5917

If you have an array of char arrays, like this:

char foo[<num>][<len>];

...then you can convert it into a string like this:

char *bar = (char *)foo;

If your strings are NULL-terminated or smaller than len, you may have to memmove() foo[i+1] to the position of strlen(bar) for each i.

Of course, it might be easier to just iterate through the array and concatenate the strings using strcat().

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385690

If you're just using standard C and the standard C library, you have to loop through and do it manually. (Of course you might use strncat in your loop.)

Upvotes: 2

Related Questions