Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!news2.google.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: Mon, 20 Feb 2012 22:16:08 -0600 Message-ID: <4F431A89.7DDD@mindspring.com> Date: Mon, 20 Feb 2012 23:16:09 -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> 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.221.31 X-Trace: sv3-O4F6VV+oWrAS8vjxcLgmQpddIVW1ZajsiraYVPr9rILMhCY5YsdwslHDD72DVz1FjIwnYmrVtc3uefw!ljPJ7FtKsp9whpcrZy4WRwDbC8SXc8dKsN4xNsJ1lQ1hX23nDPcHjBgcJ8mTMGlYBbzb5eMujdiK!2Bhj25giMQ== 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: 2445 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:17726 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); > strncat(foo, "baz", 10); ITYM strncat(baz, "baz", 10); foo, bar and baz would each have to be initialized with a null byte in order to properly use strncat on them like that. But, even if bar[3] had static duration and so was initialized with a null byte, strncat(bar, "bar", 3); writes 4 characters and overruns the array. n1570 7.24.3.2 The strncat function Synopsis 1 #include char *strncat(char * restrict s1, const char * restrict s2, size_t n); Description 2 The strncat function appends not more than n characters (a null character and characters that follow it are not appended) from the array pointed to by s2 to the end of the string pointed to by s1. The initial character of s2 overwrites the null character at the end of s1. A terminating null character is always appended to the result. -- pete