开发者

how to concatenate an indefinite number of strings without using string.h

开发者 https://www.devze.com 2023-04-12 04:44 出处:网络
So I have a char **args, but I want to concatenate all the 开发者_JAVA技巧strings into a single char *newArgs without using string.h.

So I have a char **args, but I want to concatenate all the 开发者_JAVA技巧strings into a single char *newArgs without using string.h.

Anyone know a good way to do this?


This is actually one of the few cases where disregarding the standard implementation (string.h) and building a concatenating function of your own gets you better results. See Joel's fantastic article that discusses this topic.

But back to your question: you need to do two passes through the strings in args. On the first pass you sum the lengths of the strings (don't forget null terminator!). Now that you have the length of the resulting string you can allocate a buffer for newArgs. On the other pass you copy the contents from args to newArgs.

0

精彩评论

暂无评论...
验证码 换一张
取 消