strncpy

Bjorn Engsig bengsig at oracle.nl
Fri Dec 22 01:54:10 AEST 1989


Article <11509 at csli.Stanford.EDU> by poser at csli.Stanford.EDU (Bill Poser) says:
|
|	Why is it that strncpy(3) does not null terminate  ...
strncpy always copies exactly n characters to the destination string by null-
padding or truncating.  A similar behaviour is also found in a declaration like

  char thing[][3] = { "a", "ab", "abc", "de" };

where each element of thing is an array of 3 characters (i.e. thing[2] is
not null terminated, and puts(thing[2]) would print abcde).

A way to do a limited string copy is

  dst[0] = 0;
  strncat(dst,src,n);

since strncat always null-terminates.
-- 
Bjorn Engsig,	Domain:		bengsig at oracle.nl, bengsig at oracle.com
		Path:		uunet!{mcsun!orcenl,oracle}!bengsig



More information about the Comp.lang.c mailing list