delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/26/10:08:48

Date: Sun, 26 Oct 1997 17:04:54 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Erik Max Francis <max AT alcyone DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: strncpy question
In-Reply-To: <34525ABE.54074B5A@alcyone.com>
Message-ID: <Pine.SUN.3.91.971026170428.17911R-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019