But at point 2: Does 'R' 'o' 'c' 'k' get copied over 'H' 'e' 'l' 'l',
while 'o' remains?
Yes. Read the documentation:
These functions try to copy the first D characters of strSource to strDest, where D is the lesser of count and the length of strSource. If those D characters will fit within strDest (whose size is given as numberOfElements) and still leave room for a null terminator, then those characters are copied and a terminating null is appended; otherwise, strDest[0] is set to the null character and the invalid parameter handler is invoked, as described in Parameter Validation.
Now, for your other questions:
Or is it now pointing to a new area in memory?
No, mystring
is still refers to the same array.
Do I
need to delete and recreate myString like in 3?
Perhaps. Depends on what you want to do. If you want copies of both strings, then yes, you will need two character arrays (either static or dynamic).
What if I have
copiedString2 first and then copiedString1?
In that case, you'd get Rock
as your string.
Does anything different
happen?
A different string is present in mystring
after the operations, so yes.
Anything else that might be useful to know?
Do you want to concatenate these two strings? If so, use a concatenating function, such as strcat
. Also, note that functions beginning with an underscore are non-standard, vendor specific functions and thus will not be portable in all probability. Try to use a standard defined function (such as strncpy
, strcat
, strncat
etc.).
The _t
prefixed functions in MS world are most often macros that switch to the appropriate ASCII/Unicode versions of the respective functions depending on your project settings (i.e. whether UNICODE
/_UNICODE
pre-processor macro has been defined or not).
Finally, the variations of string copy and concatenation with an n
in between reads atmost n
characters from the source. This design is allow programmers to write secure code (and thus prevent buffer overflows).
Oh, and before we forget, if you're using C++, you ought to forget all about C-style string manipulation and simply switch over to the neater, easier to use std::string
(or std::wstring
as the case maybe). String copy happens via simple assignment (i.e. =
suffices) and concatenation is equally idiomatic (i.e. +=
suffices). For more check the documentation.