prev up next   top/contents search

comp.lang.c FAQ list · Question 13.2

Q: Why does strncpy not always place a '\0' terminator in the destination string?


A: strncpy was first designed to handle a now-obsolete data structure, the fixed-length, not-necessarily-\0-terminated ``string.'' [footnote] strncpy is admittedly a bit cumbersome to use in other contexts, since you must often append a '\0' to the destination string by hand.

You can get around the problem by using strncat instead of strncpy. If the destination string starts out empty (that is, if you do *dest = '\0' first), strncat does what you probably wanted strncpy to do:

	*dest = '\0';
	strncat(dest, source, n);
This code copies up to n characters, and always appends a \0.

Another possibility is

sprintf(dest, "%.*s", n, source)
(though, strictly speaking, this is only guaranteed to work for n <= 509).

When arbitrary bytes (as opposed to strings) are being copied, memcpy is usually a more appropriate function to use than strncpy.


prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North