Mail Archives: djgpp/1997/10/26/10:08:48
On Sat, 25 Oct 1997, Erik Max Francis wrote:
> Basically, don't use strncpy or strncat except in cases where you're
> trying to fill a fixed-sized buffer, and do not care whether or not the
> result is NUL-terminated (but _do_ care if it is NUL-padded).
`strncpy' and `strncat' are not the same in this respect. A
frequently-overlooked feature of `strncat' is that it ALWAYS stores a
terminating null character. So the following fragment will always
produce a null-terminated string:
str[0] = '\0';
strncat (str, string1, n1);
etc., whereas using `strncpy' requires to append the null by hand.
IMHO, initializing the destination is a small price to pay for knowing
your strings will always be properly terminated. So I suggest using
`strncat' rather than `strncpy'.
- Raw text -