Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #4064

Re: I need help

Message-ID <4DD107C1.2723@mindspring.com> (permalink)
Date 2011-05-16 07:17 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: I need help
References <dceaf88c-7dca-487c-881a-64dcd49c76c3@glegroupsg2000goo.googlegroups.com>

Show all headers | View raw


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" <ian-...@hotmail.com> 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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: I need help janus <emekamicro@gmail.com> - 2011-05-16 03:52 -0700
  Re: I need help "Heinrich Wolf" <invalid@invalid.invalid> - 2011-05-16 13:14 +0200
  Re: I need help pete <pfiland@mindspring.com> - 2011-05-16 07:17 -0400
  Re: I need help James Kuyper <jameskuyper@verizon.net> - 2011-05-16 07:27 -0400
  Re: I need help Ian Collins <ian-news@hotmail.com> - 2011-05-17 09:05 +1200

csiph-web