Is there an easy way to copy C strings?
I have const char *stringA, and I want char *stringB to take the value (note that stringB is not const). I tried stringB=(char*) stringA, but that makes stringB still point to the same memory location, so when stringA later changes, stringB does too.
I’ve also tried strcpy(stringB,stringA), but it seems that if stringB wasn’t initialized to a large enough array, there’s a segfault. I’m not super experienced with C strings though, am I missing something obvious?
If I just initialize stringB as char *stringB[23], because I know I’ll never have a string longer than 22 characters (and allowing for the null terminator), is that the right way? If stringB is checked for equality with other C-strings, will the extra space affect anything?
(And just using strings isn’t a solution here, as I need minimal overhead and easy access to individual characters.)
You could use
strdup()to return a copy of a C-string, as in:You could also use
strcpy(), but you need to allocate space first, which isn’t hard to do but can lead to an overflow error, if not done correctly:If you cannot use
strdup(), I would recommend the use ofstrncpy()instead ofstrcpy(). Thestrncpy()function copies up to — and only up to —nbytes, which helps avoid overflow errors. Ifstrlen(stringA) + 1 > n, however, you would need to terminatestringB, yourself. But, generally, you’ll know what sizes you need for things:I think
strdup()is cleaner, myself, so I try to use it where working with strings exclusively. I don’t know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.Note that I cast the result of
malloc()tochar *. This is because your question is tagged as ac++question. In C++, it is required to cast the result frommalloc(). In C, however, you would not cast this.EDIT
There you go, there’s one complication:
strdup()is not in C or C++. So usestrcpy()orstrncp()with a pre-sized array or amalloc-ed pointer. It’s a good habit to usestrncp()instead ofstrcpy(), wherever you might use that function. It will help reduce the potential for errors.