|
From: Jason H. <jas...@bt...> - 2001-10-19 14:42:08
|
I can say for certain that strcat() won't ever include any extra '\0' into a
string. If it did it would be due to a bug in the routine. The function is
passed a pointer to the beginning of two strings, so the only way it could
start at the end of a string would be to look for it. The only way it can
determine the end is to find the null terminator, which will be the first 0
it finds. It won't look beyond that zero, so won't see any other text or
zero's that may exist. Once it has found the end of the first string it will
add on the characters from the second, overwring the null terminator in the
first and any other stuff that is there. It will go on till it finds the
NULL on the second string and will be sure to add a NULL onto the
concatenated string. Here is a short function that probably does the same as
the standard one.
MyStrCat(char *str1, const char *str2)
{
while(*str1) ++str1; // find end of first string
// out now points to the null of out
while(*str2)
*str1++ = *str2++; // copy second string
// to end of first.
*str1 = 0; // null terminate
}
Note that if you pass two pointers to the same string you will get an
infinate loop.
Regards Jason.
PS: guess I've gone OTT again.... oops!
----- Original Message -----
From: "Ioannis Vranos" <no...@ya...>
To: "Dev-C++" <dev...@li...>
Sent: Friday, October 19, 2001 10:25 AM
Subject: RE: [Dev-C++] Errorhandling
> Any string literal will be automatically NULL terminated
> anyway, but putting in the extra NULL wouldn't cause any harm
> as strcat would just ignore it. A C++ string would be better,
We do not know if it would ignore it, it could end up in a concatenated
string having a double '\0', and in the next call of strcat() it could
end up in something like that:
[stringonestringtwo\0stringthree...\0]
That is we do not know how a strcat() implementation works (perhaps from
the end towards the beginning) and it could replace the last '\0' only.
In any case that '\0' is not needed.
|