3 ms·
When I used C for a serious project, I always used `snprintf(dst, sizeof(dst), "%s", src);` to copy a string. It might be a little bit slow, but it freed me fro
by barosl 5y ago
When I used C for a serious project, I always used `snprintf(dst, sizeof(dst), "%s", src);` to copy a string. It might be a little bit slow, but it freed me from all the headaches of identifying different string functions of C and remembering their subtle differences. It also is useful for other purposes, e.g. prefixing a string.
- AlexanderDhoore 5y agoI do this as well. At some point I googled around and didn't get a straight answer. So I've been using snprintf() for all my string manipulation ever since. My productivity is more important than a sliver of performance.
- mlindner 5y agoDon't use sizeof. If you pass in a pointer you're going to get the size of the pointer, not the length of the string buffer.
- tedunangst 5y agoBut no suggestion for an alternative?
- mlindner 5y agoThere's a lot of alternatives, none of them I particularly like best for all cases. So I won't suggest something I don't think is very good. sizeof is particularly bad however for any kind of strings.
- savannape 5y agosizeof would give you the actual size if you pass in a statically declared array.