简体   繁体   中英

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

So I have a char **args , but I want to concatenate all the 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM