Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 25 Feb 2012 09:34:30 -0600 Message-ID: <4F48FF8A.6666@mindspring.com> Date: Sat, 25 Feb 2012 10:34:34 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: strncpy and 'n' References: <58d7c42a-7fd9-4507-bbe6-88941700b88a@k6g2000vbz.googlegroups.com> <4F431A89.7DDD@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 47 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.222.31 X-Trace: sv3-ayZ/Ri9D11GHkaGtETdBhQyAdph6xCJ7TsEpERhFZlK6YDvyNAH7JZDdrhrbgP9i2rFoYdHHZrt7jxn!UmdGC7R8FZKDllBUBuKB+CKQoE7iMI1AV7R/1AYCiFmaxbg0CR16AV7Cb3Ay9+/OvgIZ2nIa1OsE!Ai5BMyINzg== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2560 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:17929 Shao Miller wrote: > > On 2/20/2012 23:16, pete wrote: > > Shao Miller wrote: > > > >> My impression is that in: > >> > >> char foo[] = "foo"; > >> char bar[3] = "bar"; > >> char baz[10] = "baz"; > >> > >> each of these could be roughly equivalent to: > >> > >> char foo[sizeof "foo"]; > >> char bar[3]; > >> char baz[10]; > >> > >> strncat(foo, "foo", sizeof "foo"); > >> strncat(bar, "bar", 3); > Somehow, I completely was confusing 'strncpy' with 'strncat', here. :( > Please substitute 'strncpy' in place of 'strncat' in my post. :( Of > course, that makes it irrelevant > to Keith's immediately-preceding post. > > Thank you for the correction, pete! You're welcome. What I don't about the idea of using strncpy instead of strcpy, is that learning to use strncpy instead of strcpy, seems to me to be more complicated than learning to use strcpy properly. The n parameter of strncpy makes it harder to forget that the size of something must be taken into account. But anyone who would have trouble remembering how to use strcpy correctly, might also have trouble remembering that if n is less than (1 + source string length) then the resulting strncpy write, will not be a string. I've always been comfortable using strcpy. -- pete