Notice that when you declare strC and strD, you've actually declared arrays of
pointers to chars, not chars. I have to assume you simply typed that wrong, because it would never compile the way you've coded it. The proper way to declare them is:
1 2
|
char strC[50];
char strD[50];
|
(EDIT: whoops, you corrected this already while I was typing.)
Saying that the destination string is "empty" is a common beginner's misconception. Really, like Albatross said, there's already a bunch of garbage there - that is, there are some random 1's and 0's in those slots of memory which are simply not being used by your computer, and were allocated by your program to your char array. To truly "empty" that block, you have to overwrite those characters yourself. This is often done with:
1 2 3
|
memset(strDest,0,50);
|
...where '50' can be whatever size you need to "empty out", in bytes. It should not exceed the amount you allocated, though. The zero, when used in a string, is the "null terminator". In char form, it is: '\0'. It simply tells C where the character content of a string should actually end in memory, even though the memory allocated to the string may be greater.
When you allocate a string using a "string literal", like this:
|
char *strOne = "Test string";
|
... a terminating null character is automatically added to the end of the string, but when you allocate it like this:
... no terminating null character is appended. strcat() would look for the first '\0' in this string, and it could be anywhere. In your case, there's one right after the junk you're seeing, and that's where the function appends the source string.
It's also entirely possible that there is no zero anywhere, in which case strcat() would go past 50 when looking for the end of the destination string. That would likely result in a runtime error. So always be sure to "empty" the strings yourself before working with them.