[chbot] !!!!!!!! Choc fish challenge 2 !!!!!!!!!

Charles Manning manningc2 at actrix.gen.nz
Thu Aug 4 02:08:31 BST 2011


As Andre said, the original code is equivalent to the lop in strcpy().

A full strcpy function can be made with:

char *strcpy(cahr *s1, const char *s2)
{
	char *x = s1;

	while(*s1++ = *s2++);
	return x;
}

Whether or not strcpy() will terminate depends on the string. Hence the 
introduction of safer versions like strncpy().

Note however that 

char *strcpy(cahr *s1, const char *s2)
{
	char *x = s1;

	while(*s1++ == *s2++);
	return x;
}

wouldgive you an entirely different result.

while(*x++ = *y++);
does the following
 Assign *y to *x then increments x and y then if the result of the assignment 
was non-zero, loop.
The looping continues until the copied character is zero (ie. end of string).

while(*s++ == *s2++);
 does the following:
 Compare *x with *y, then increment x and y then if the comparison was true 
then loop.
The looping continues until *x and *y do not match.

See why we shoot people that write code like this!



On Thursday 04 August 2011 12:45:33 Pieter Britz wrote:
> Or it will run until you run out of ram or rom space, assuming you can
> access all of the ram/rom space you are pointing to.
> ...
> Just thought of something, the code does not say while((*x++ *==* *y++)){},
> thus the value y is pointing to, incremented by one, should always be able
> to *successfully *be stored in the register x is pointing to, even if the
> value in *x is incremented before the store.
> Thus == to while(1){}

>
> Cheers,
> Pieter.
>
> On Thu, Aug 4, 2011 at 8:53 AM, Andre Renaud <andre at bluewatersys.com> wrote:
> > On 03/08/11 22:13, Volker Kuhlmann wrote:
> > > On Wed 03 Aug 2011 11:40:03 NZST +1200, Charles Manning wrote:
> > >> In my books there is also a very hot place reserved for those who
> > >> write
> >
> > things
> >
> > >> like
> > >> while((*x++ = *y++)){}
> > >
> > > You can't really compare this with previous examples. This one is much
> > > different because it is undefined, unpredictable and therefore useless
> > > and much worse. Your choc fish challenge was defined in its behaviour
> > > and predictable, although extremely bad programming and a convoluted
> > > way to say while(1); .
> >
> > I don't think this is undefined - isn't this a simple implementation of
> > strcpy (assuming x & y are char *)? It will continue around until it
> > assigns a 0 pointer, at which point it will stop. It would be undefined
> > if you had *x++ = *x++.
> >
> > Regards,
> > Andre
> >
> > _______________________________________________
> > Chchrobotics mailing list Chchrobotics at lists.linuxnut.co.nz
> > http://lists.ourshack.com/mailman/listinfo/chchrobotics
> > Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
> > Web site: http://kiwibots.org
> > Meetings 3rd Monday each month at Tait Radio Communications, 175 Roydvale
> > Ave, 6.30pm
> >
> > When replying, please edit your Subject line to reflect new content.





More information about the Chchrobotics mailing list