Path: csiph.com!x330-a1.tempe.blueboxinc.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: Mon, 16 May 2011 06:17:24 -0500 Message-ID: <4DD107C1.2723@mindspring.com> Date: Mon, 16 May 2011 07:17:21 -0400 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: I need help References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 89 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.223.81 X-Trace: sv3-fFbCGA2G1hcqjPvpZu9bOAo82QCkxiSegjffGqIbIX677TXcjIh7Ta0wfPKjGi0pv8PIMF42oAobAGz!E8q6wZuZBmUcnm/CdhN13y6ydOd+d0NW4HT6tZ01Bn7p5uiSj4leNIlrKZX42eRl5ahOWSaraVie!Tm7G/Tjtmw== 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: 3014 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:4064 janus wrote: > > > -- > > Ian Collins > You must not use the macro STRNCAT in e.g. this case: > > void a_procedure(char *a) > { > STRNCAT(a, "Jersey"); > } > > int main(void) > { > > char string[10] = "NEW"; > a_procedure(string); > } > > Here sizeof(a) is the size of a pointer > and not the size of string. Instead > you could do it without the macro: > > void a_procedure(char *a, size_t size) > { > strncat(a, "Jersey", size - strlen(a) - 1); > } > > int main(void) > { > > char string[10] = "NEW"; > a_procedure(string, sizeof(string)) > > On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote: > > "Ian Collins" schrieb im Newsbeitrag > > news:93bdoaFlpiU2@mid.individual.net... > > > On 05/15/11 06:29 AM, Heinrich Wolf wrote: > > >> > > >> or with a macro: > > >> > > >> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1) > > >> char string[10] = "NEW"; > > >> STRNCAT(string, "Jersey"); > > > > > > That macro would go horribly (and silently) wrong if > > > > > > a) 'a' wasn't a fixed sized array, or > > > b) it was, but the definition wasn't visible > > > when the macro was expanded. > > > > > > -- > > > Ian Collins > > > > You must not use the macro STRNCAT in e.g. this case: > > > > void a_procedure(char *a) > > { > > STRNCAT(a, "Jersey"); > > } > > > > int main(void) > > { > > > > char string[10] = "NEW"; > > a_procedure(string); > > } > > > > Here sizeof(a) is the size of a pointer > > and not the size of string. Instead > > you could do it without the macro: > > > > void a_procedure(char *a, size_t size) > > { > > strncat(a, "Jersey", size - strlen(a) - 1); > > } > > > > int main(void) > > { > > > > char string[10] = "NEW"; > > a_procedure(string, sizeof(string)); > > } > > I do not see any difference between the two Try harder. -- pete